socket.c
上传用户:sdtbys
上传日期:2009-12-06
资源大小:13k
文件大小:8k
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <signal.h>
- #include <unistd.h>
- #define sockOpenFail -12001
- #define sockBindFail -12002
- #define sockGetSockName -12003
- #define sockSendFail -12004
- #define sockRecvFail -12005
- #define sockUnknowHost -12006
- #define sockConnectFail -12007
- #define sockAcceptFail -12008
- #define sockUnknowServ -12009
- #define sockgetnameFail -12010
- #define sockgetpeerFail -12011
- #define sockListenError -12012
- int SockClientOpen(char *pszHost,char *sinPort);
- int sockClientOpen(char *pszHost,int sinPort);
- int SockServerOpen(char *sinPort,int Listenum);
- int sockServerOpen(int sinPort, int Listenum);
- int sockClose(int sHandle);
- int sockSend(int sHandle,char *pszBuf,int iLen);
- int sockRecv(int sHandle,char *pszBuf,int iMaxLen);
- int sockListen(int sHandle);
- int sockLook(int sHandle);
- extern int errno;
- extern char **environ;
- /*static int Listensock;*/
- int SockClientOpen( char *pszHost, char *sinPort )
- {
- int sHandle;
- u_long hl;
- struct sockaddr_in server;
- struct hostent *hp,*gethostbyname();
- struct servent *sp,*getservbyname();
-
- if( ( sHandle = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 )
- {
- ShowMsg("clinetopen:socket : %d,%sn",errno,strerror(errno));
- return sockOpenFail;
- }
- memset((char *)&server,0,sizeof(struct sockaddr_in));
- server.sin_family=AF_INET;
- /*server.sin_port = htons(sinPort);*/
- if( ( sp = getservbyname(sinPort,"tcp"))==NULL )
- {
- ShowMsg("getservbyname : %d,%sn",errno,strerror(errno));
- return sockUnknowServ;
- }
- server.sin_port =sp->s_port;
- /*hl=inet_addr(pszHost);
- memcpy( (char *)&server.sin_addr, (char *)&hl, 4 ); */
- if( ( hp = gethostbyname( pszHost ) ) == NULL )
- {
- ShowMsg("gethostbyname : %d,%sn",errno,strerror(errno));
- return sockUnknowHost;
- }
- memcpy( (char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length );
- if( connect( sHandle, (struct sockaddr *)&server, sizeof(server) ) < 0 )
- {
- ShowMsg("connect : %d,%sn",errno,strerror(errno));
- close(sHandle);
- return sockConnectFail;
- }
- return( sHandle );
- }
- int sockClientOpen( char *pszHost, int sinPort )
- {
- int sHandle;
- u_long hl;
- struct sockaddr_in server;
- struct hostent *hp,*gethostbyname();
- struct servent *sp,*getservbyname();
-
- if( ( sHandle = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 )
- {
- ShowMsg("clinetopen:socket : %d,%sn",errno,strerror(errno));
- return sockOpenFail;
- }
- memset((char *)&server,0,sizeof(struct sockaddr_in));
- server.sin_family=AF_INET;
- server.sin_port = htons(sinPort);
- /*if( ( sp = getservbyname(sinPort,"tcp"))==NULL )
- {
- ShowMsg("getservbyname : %d,%sn",errno,strerror(errno));
- return sockUnknowServ;
- }
- server.sin_port =sp->s_port;*/
- hl=inet_addr(pszHost);
- memcpy( (char *)&server.sin_addr, (char *)&hl, 4 );
- /*if( ( hp = gethostbyname( pszHost ) ) == NULL )
- {
- ShowMsg("gethostbyname : %d,%sn",errno,strerror(errno));
- return sockUnknowHost;
- }
- memcpy( (char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length );
- */
- if( connect( sHandle, (struct sockaddr *)&server, sizeof(server) ) < 0 )
- {
- ShowMsg("connect : %d,%sn",errno,strerror(errno));
- close(sHandle);
- return sockConnectFail;
- }
- return( sHandle );
- }
- int SockServerOpen( char *sinPort,int Listenum)
- {
- int sHandle;
- int iLen;
- /*int LISTEN;*/
- struct sockaddr_in server;
- struct servent *sp,*getservbyname();
- /*LISTEN=atoi(getenv("LISTEN"));*/
- /*if (LISTEN ==0) LISTEN=20;*/
- /*if (LISTEN <= 0)
- {
- ShowMsg("env :LISTEN not found or errorn");
- return sockListenError;
- }*/
- if( ( sHandle = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
- {
- ShowMsg("sockcomm : %d,%sn",errno,strerror(errno));
- return sockOpenFail;
- }
- memset((char *)&server, 0, sizeof(struct sockaddr_in));
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = INADDR_ANY;
- /* server.sin_port = htons(sinPort); */
- if( ( sp = getservbyname(sinPort,"tcp"))==NULL )
- {
- ShowMsg("getservbyname : %d,%sn",errno,strerror(errno));
- return sockUnknowServ;
- }
- server.sin_port =sp->s_port;
- if( bind( sHandle, ( struct sockaddr * )&server, sizeof(server) ) < 0 )
- {
- ShowMsg("sockcomm : %d,%sn",errno,strerror(errno));
- return sockBindFail;
- }
- iLen = sizeof( server );
- if( getsockname( sHandle,( struct sockaddr * )&server, &iLen ) < 0 )
- {
- ShowMsg("sockcomm : %d,%sn",errno,strerror(errno));
- return sockGetSockName;
- }
- listen(sHandle,Listenum);
- return (sHandle);
- }
- int sockServerOpen( int sinPort,int Listenum)
- {
- int sHandle;
- int iLen;
- /*int LISTEN; */
- struct sockaddr_in server;
- struct servent *sp,*getservbyname();
- /*LISTEN=atoi(getenv("LISTEN"));*/
- /*if (LISTEN ==0) LISTEN=20;*/
- /*if (LISTEN <= 0)
- {
- ShowMsg("env :LISTEN not found or errorn");
- return sockListenError;
- }*/
- if( ( sHandle = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
- {
- ShowMsg("sockcomm : %d,%sn",errno,strerror(errno));
- return sockOpenFail;
- }
- memset((char *)&server, 0, sizeof(struct sockaddr_in));
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = INADDR_ANY;
- server.sin_port = htons(sinPort);
- /*if( ( sp = getservbyname(sinPort,"tcp"))==NULL )
- {
- ShowMsg("getservbyname : %d,%sn",errno,strerror(errno));
- return sockUnknowServ;
- }
- server.sin_port =sp->s_port;*/
- if( bind( sHandle, ( struct sockaddr * )&server, sizeof(server) ) < 0 )
- {
- ShowMsg("sockcomm : %d,%sn",errno,strerror(errno));
- return sockBindFail;
- }
- iLen = sizeof( server );
- if( getsockname( sHandle,( struct sockaddr * )&server, &iLen ) < 0 )
- {
- ShowMsg("sockcomm : %d,%sn",errno,strerror(errno));
- return sockGetSockName;
- }
- listen(sHandle,Listenum);
- return (sHandle);
- }
- int sockClose( int sHandle )
- {
- close( sHandle );
- return( 0 );
- }
- int sockSend( int sHandle, char *pszBuf, int iLen )
- {
- int iRet;
- iRet = send( sHandle, pszBuf, iLen, 0 );
- if( iRet < 0 ) {
- ShowMsg("sockcomm : %d,%sn",errno,strerror(errno));
- return( sockSendFail );
- }
- return( 0 );
- }
- int sockRecv( int sHandle, char *pszBuf, int iMaxLen )
- {
- int iRet;
- iRet = recv( sHandle, pszBuf, iMaxLen, 0 );
- if( iRet < 0 ) {
- ShowMsg("sockcomm : %d,%sn",errno,strerror(errno));
- return( sockRecvFail );
- }
- return ( iRet );
- }
- int sockListen( int sHandle )
- {
- int iSock;
- iSock = accept( sHandle, ( struct sockaddr * )NULL, ( int * )NULL );
- if ( iSock == -1 )
- {
- ShowMsg("sockcomm : %d,%sn",errno,strerror(errno));
- return( sockAcceptFail );
- }
- return(iSock);
- }
- int sockLook(int sHandle)
- {
- struct sockaddr_in server;
- int len,ret;
- char sHostAddr[20];
- int port;
- len=sizeof(server);
- ret=getsockname(sHandle,(struct sockaddr * ) &server,&len);
- if ( ret != 0)
- {
- ShowMsg("sockLook : %d,%sn",errno,strerror(errno));
- return( sockgetnameFail );
- }
- sprintf(sHostAddr, "%s", inet_ntoa(server.sin_addr));
- port=ntohs(server.sin_port);
- ShowMsg("local ip:port = %s:%d n",sHostAddr,port);
- ret=getpeername(sHandle,(struct sockaddr * ) &server,&len);
- if ( ret != 0)
- {
- ShowMsg("sockLook : %d,%sn",errno,strerror(errno));
- return( sockgetpeerFail );
- }
- sprintf(sHostAddr, "%s", inet_ntoa(server.sin_addr));
- port=ntohs(server.sin_port);
- ShowMsg("remote ip port = %s:%d n",sHostAddr,port);
-
- }