快速业务通道

Linux 下tcp多线程编程

作者 佚名技术 来源 Linux系统 浏览 发布时间 2012-04-02
t;get msg head failed !n");
continue;
}

iBodyLen = 0;

Empire CMS,phome.net

MsgHead = (MH *)RecvBuf;
iType = (int)MsgHead->type;
//printf("type :%dn",iType);
if (2 == iType)
{
printf("nServer tell:client %d exit!nn",fd);
//sleep(10);
fprintf(fp,"%sn","commit;");
fclose(fp);
close(fd);
return 0;
}

iBodyLen = MsgHead->BL[0] * 256 * 256 * 256;
iBodyLen = MsgHead->BL[1] * 256 * 256;
iBodyLen = MsgHead->BL[2] * 256;
iBodyLen = MsgHead->BL[3];
//printf("BodyLen=%dn",iBodyLen);

memset(RecvBuf,0X00,sizeof(RecvBuf));
iRet = tcp_recv(fd,iBodyLen,RecvBuf);
if (iRet == iBodyLen)
{
RecvBuf[iBodyLen] = '''';
if (1000 > iRecCount)
{
fprintf(fp,"%sn",RecvBuf);
iRecCount ;
}
else
{
/* 本文件已写满1000 条记录 */
fprintf(fp,"%sn","commit;");
fclose(fp);

/* 重新打开一个文件写记录 */
iRecCount = 0;
memset(sFileName,0x00,sizeof(sFileName));

pthread_mutex_lock(&mutex);
GetFileName(sFileName);
pthread_mutex_unlock(&mutex);
printf("Filename:%sn",sFileName);

if ( 0 > strlen(sFileName))
{
printf("get filename failedn");
return NULL;
}
fp = fopen(sFileName,"w ");
fprintf(fp,"%sn","set termout on");
fprintf(fp,"%sn","set feed off");

fprintf(fp,"%sn",RecvBuf);
iRecCount ;
}

}
}
return 0;
}

void sig_handler(int signo)

Empire CMS,phome.net

{
switch (signo)
{
case SIGTERM:
case SIGINT:
printf("received SIGTERM or SIGINT(tid:%u)", (unsigned int)pthread_self());
break;

case SIGHUP:
printf("received SIGHUP(tid:%u)", (unsigned int)pthread_self());
break;

case SIGUSR1:
printf("received SIGUSR1(tid:%u)", (unsigned int)pthread_self());
break;


case SIGPIPE:
printf("received SIGPIPE(tid:%u)", (unsigned int)pthread_self());
break;

default:
printf("received signal %d(tid:%u)", signo, (unsigned int)pthread_self());
break;
}
}

int setup_sig_handlers(void)
{
struct sigaction act;

act.sa_handler = sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGTERM,&act,NULL);
sigaction(SIGINT, &act, NULL);
sigaction(SIGHUP, &act, NULL);
sigaction(SIGUSR1,&act,NULL);
sigaction(SIGUSR2,&act,NULL);
sigaction(SIGPIPE, &act, NULL);

return 0;
}

int main()
{
struct sockaddr_in SerAddr,CliAddr;
int serfd = -1,clfd = -1,iSize = -1,count = 0;
int bufSize = 4 * 1024 * 1024;
pthread_t tid[256];

pthread_attr_init(&attr);
pthread_mutex_init(&mutex,NULL);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

setup_sig_handlers();

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号