t;get msg head failed !n");
continue;
}
iBodyLen = 0;
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)
{
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();
|