main.c
==========================
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <fcntl.h>
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
#include "sockcomm.h"
#define TimeOut 30
static int iSockID1;
static int iSockID;
static int iSock;
typedef void sigfunc( int );
jmp_buf tohere2;
int CloseSock();
main()
{
int iRet,p,p1,len,pid;
int iPort,jPort,packlen;
int iSndLen,iRcvLen,retcode;
long mtype;
char type[7];
char szBuf[1028];
char rzBuf[1028];
char szHost[20],szSnd[1028],szRcv[1028];
char requ[1025],resp[1028];
char msgbuf[1028];
char head;
sigfunc *sig1;
Record *rec;
MsgId = msg_init();
mtype = 6L;
iPort = 8300;
iSockID = sockServerOpen( iPort );
if( iSockID < 0 )
{
printf("SockServerOpen FAIL! ret_code = %dn",iSockID);
exit(0);
}
signal(SIGCLD,SIGIGN);
signal(SIGINT,CloseSock);
while(1)
{
memset( szBuf,' ',1028);
iSock = sockListen( iSockID );
if( iSock < 0 ) break;
iRet = sockRecvBuf( iSock, szBuf, 4 ,TimeOut );
if(iRet<=0)
{
printf("nn接收网点socket失败!n");
close(iSock);
continue;
}
len=atoi(szBuf);
iRet = sockRecvBuf( iSock, szBuf, len ,TimeOut );
if(iRet<=0)
{
printf("nn接收网点socket失败!n");
close(iSock);
continue;
}
printf("nn接收网点socket:[%s]n",szBuf);
if((pid=fork())<0 )
{
printf("Fork Error!");
close(iSock);
continue;
}
else if( pid>0 )
{
strcpy( szHost,"192.168.0.1" );
jPort = 5555;
memset( szRcv,0,sizeof(szRcv) );
iSockID1=sockClIEntOpen( szHost, jPort);
/***** Begin to Send *****/
packlen = htons(strlen(szBuf));
iRet=sockSendBuf( iSockID1, (char *)&packlen,2,TimeOut);
if(iRet<0)
{
printf("发送socket ERROR 1!n");
close(iSockID1);
close(iSock);
exit (-1);
}
len = strlen(szBuf);
iRet=sockSendBuf( iSockID1, szBuf,len,TimeOut);
if(iRet<0)
{
printf("发送socket ERROR 2!n");
close(iSockID1);
close(iSock);
exit (-1);
}
/***** End of Send *****/
/***** Begin to Recv Return Code *****/
iRet=sockRecvBuf( iSockID1,(char *)&len,2 ,TimeOut);
if(iRet<0)
{
printf("接收socket ERROR 1!n");
close(iSockID1);
close(iSock);
exit (-1);
}
len=ntohs(len);
memset(szRcv,0,sizeof(szRcv));
iRet=sockRecvBuf( iSockID1, szRcv,len,TimeOut);
if(iRet<0)
{
printf("接收socket ERROR 2!n");
close(iSockID1);
close(iSock);
exit (-1);
}
printf("接收socket:[%s]n",szRcv);
/***** End of Recv Return Code *****/
memset(szBuf,' ',1028);
sprintf(szBuf,"%04d",len);
iRet=sockSendBuf(iSock,szBuf,4,TimeOut);
if(iRet<0)
{
printf("发送socket至网点失败1!n");
close(iSockID1);
close(iSock);
exit(-1);
}
iRet=sockSendBuf(iSock,szRcv,len,TimeOut);
if(iRet<0)
{
printf("发送socket至网点失败2!n");
close(iSockID1);
close(iSock);
exit (-1);
}
printf("发送socket至网点成功!n");
}
close(iSock);
}
}
int CloseSock()
{
close(iSock);
close(iSockID);
close(iSockID1);
return 0;
}
comm.c 通讯函数
=============================
#include <math.h>
#include <stdio.h>
#include <sys/tape.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
typedef void sigfunc( int );
jmp_buf tohere1;
void alrm1(int a)
{
alarm(0);
longjmp( tohere1, 1 );
}
int sockServerOpen( int sinPort )
{
int sHandle;
int iLen;
struct sockaddr_in server;
if( ( sHandle = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
return sockOpenFail;
server.sin_family = AF_INET;
server.sin_addr.s_addr = ntohl(INADDR_ANY);
server.sin_port = ntohs(sinPort);
if( bind( sHandle, ( struct sockaddr * )&server, sizeof(server) ) < 0 )
return sockBindFail;
iLen = sizeof( server );
if( getsockname( sHandle,( struct sockaddr * )&server, &iLen ) < 0 )
{
return sockGetSockName;
}
listen(sHandle,5);
return (sHandle);
}
int sockClientOpen( char *pszHost, int sinPort ,int timeout)
{
int sHandle;
sigfunc *sig;
struct sockaddr_in server;
struct hostent *hp,*gethostbyname();
if( setjmp( tohere1 ) )
{
signal( SIGALRM, sig);
return ( sockOpenFail );
}
sig=signal( SIGALRM, alrm1);
alarm( timeout );
if( ( sHandle = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
return sockOpenFail;
server.sin_family=AF_INET;
/*
if( ( hp = gethostbyname( pszHost ) ) == NULL )
{
if( ( hp = gethostbyaddr(pszHost,strlen(pszHost),SOCK_STREAM) ) == NULL )
{
alarm( 0 );
signal( SIGALRM, sig);
return sockUnknowHost;
}
}
memcpy( (char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length );
*/
server.sin_addr.s_addr=inet_addr(pszHost);
server.sin_port = htons(sinPort);
if( connect( sHandle, (struct sockaddr *)&server, sizeof(server) ) < 0 )
{
alarm( 0 );
signal( SIGALRM, sig);
return sockConnectFail;
}
alarm( 0 );
signal( SIGALRM, sig);
return( sHandle );
}
int sockClose( int sHandle )
{
close( sHandle );
return( 0 );
}
int sockSendBuf( int sHandle, void *pszBuf, int iLen ,int iTimeOut)
{
int iRet=0;
sigfunc *sig;
int i=0;
if( setjmp( tohere1 ) )
{
signal( SIGALRM, sig);
return ( sockRecvTimeOut );
}
sig=signal( SIGALRM, alrm1);
alarm( iTimeOut );
while(iRet<iLen)
{
if((i=send(sHandle, pszBuf, iLen-iRet, 0))<0)
{
alarm(0);
signal(SIGALRM,sig);
return(i);
}
iRet=iRet+i;
pszBuf=(char *) pszBuf+i;
}
alarm( 0 );
signal( SIGALRM, sig);
if( iRet < 0 ) return( sockSendFail );
return( iRet );
}
int sockRecvBuf( int sHandle, void *pszBuf, int iMaxLen,int iTimeOut )
{
int iLen=0,i=0;
sigfunc *sig;
if( setjmp( tohere1 ) )
{
signal( SIGALRM, sig);
return ( sockRecvTimeOut );
}
sig=signal( SIGALRM, alrm1);
alarm( iTimeOut );
while(iLen<iMaxLen)
{
i = recv( sHandle, pszBuf, iMaxLen-iLen,0);
if (iLen < 0 )
{
alarm( 0 );
signal( SIGALRM, sig);
return (i);
}
iLen+=i;
pszBuf=(char *)pszBuf+i;
}
alarm( 0 );
signal( SIGALRM, sig);
return( iLen );
}
int sockListen( int sHandle )
{
int iSock;
do
{
iSock = accept( sHandle, ( struct sockaddr * )NULL, ( int * )NULL );
} while(iSock==-1&&EINTR==errno);
return(iSock);
}
标签: