这是我三年前写的,有的地方还不成熟,高手们见笑了!
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 = %d\n",iSockID);
exit(0);
}
signal(SIGCLD,SIGIGN);
signal(SIGINT,CloseSock);
while(1)
{
memset( szBuf,0,1028);
iSock = sockListen( iSockID );
if( iSock < 0 ) break;
iRet = sockRecvBuf( iSock, szBuf, 4 ,TimeOut );
if(iRet<=0)
{
printf("\n\n接收网点socket失败!\n");
close(iSock);
continue;
}
len=atoi(szBuf);
iRet = sockRecvBuf( iSock, szBuf, len ,TimeOut );
if(iRet<=0)
{
printf("\n\n接收网点socket失败!\n");
close(iSock);
continue;
}
printf("\n\n接收网点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 *****/
| 论坛热门帖子: | [lch203] 写得蛮好的linux学习笔记(10-21) [黑马制造] 学习java的30个目标(10-19) [笑傲股林] 做测试半年了,有点迷茫,应该再学些什么提高自己的测试水平和测试能力呢?(10-19) [udp8589] 大家用google的来吱一声? 用百度的~~也来报道下?(10-18) [沂偌掳兆] 本人总结的一些认为C++比较经典的书籍,希望对大家有用(10-18) |
| TAG标签: | 通讯 业务 中间 #59 回复 if iSock close quot signal |
注册
个人空间
