- 论坛徽章:
- 0
|
本帖最后由 unix_neu 于 2010-04-06 17:04 编辑
我编写了一个压力测试模块,实现是创建多个子进程,每个子进程循环发送多次交易信息,现在我参数定位10个子进程,每个循环发送10次,结果服务 器却未能收到100条记录。发送到一定数量后,就停止发送。实在不明白为什么,希望各位大虾给予解答!!在线等待!!
下面是源码:- int main(int argc, char *argv[])
- {
- int nRet,i=0,j=0,itime=0;
- char saSleep[20];
- char sPwd[7];
- struct sockaddr_in Sin;
- int socket_id, Flag, Error, RetryTimeSap = 2, nRetryFlag = 0;
- unsigned Port = 32005;
- char Ip_addr[] = "11.1.160.131";
- char sCfgName[30];
- char sDataName[30];
- char sMsgGmInq[1024];
- char sMsgAtmInq[1024];
- char sMsgGmCwd[1024];
- char sMsgAtmCwd[1024];
- char sMsgXfr[1024];
- char sMsgDps[1024];
- char sMsgDcl[1024];
- char sMsgATMCwc[1024];
- char sMsgGMCwc[1024];
- char sHostSsn[9];
- char sOrigSsn[9];
- char sSendName[10];
- struct sigaction sa;
- pid_t pid;
- //char filename[10];
- int lssn, iNum = 0;
- char c;
- char *p;
- int k;
- int m, sTIMES;
- char sNetMsgBuf[BUF_SIZE];
- char rspCode[3];
- char sDataBuf[4000];
- int times; //每个子进程循环发送迭代
- int totle;
- int totle_w;
- int totle_r, nums;
- GetProfileString("SSN", "HOSTSSN", sHostSsn, "Ssn.cfg");
- nums = atoi(argv[1]);
- sTIMES = atoi(argv[2]);
-
-
- sa.sa_handler=SIG_IGN;
- sigaction(SIGPIPE,&sa,0);
- memset(&Sin, 0, sizeof(Sin));
- memset(sCfgName,0,sizeof(sCfgName));
- Sin.sin_port = htons(Port);
- Sin.sin_family = AF_INET;
- Sin.sin_addr.s_addr = inet_addr(Ip_addr);
- strcpy(sSendName,"[1021]");
-
-
- printf("Connect to %s at port %d!\n", Ip_addr, Port);
- sHostSsn[8] = 0;
- //printf("Begin sHostSsn=%s\n",sHostSsn);
- k=0;
- strcpy(sSendName,"[1211]");
- int nRc = Read8583Cfg(strcpy(sCfgName,FILE_CFG));
- if( nRc < 0 )
- {
- printf("读取8583配置文件[%s]出错。\n",sCfgName);
- return;
- }
- //printf("8583配置文件[%s]。\n",sCfgName);
- /* 读测试数据 */
- for(;i<nums;i++)
- {
- if((pid=fork())==0)
- {
- m=0;
- printf("成功创建第%d个进程\n",i+1);
- totle_w=0;
- totle_r=0;
- memset(terminal_code, 0, sizeof(terminal_code));
- sprintf(terminal_code, "%08d", i+1);
- for(times=0;times<sTIMES;times++)
- {
- while(1)
- {
- while ((socket_id = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- printf("[%s][%d];socket Error!\n", __FILE__, __LINE__);
- if (nRetryFlag++ == 5)
- {
- if ( RetryTimeSap<100) RetryTimeSap += 2;
-
- nRetryFlag = 0;
- }
- sleep(RetryTimeSap);
- }
-
- Setsokopt(socket_id);
- if (connect(socket_id, (struct sockaddr *)&Sin, sizeof(Sin)) < 0)
- {
- printf("[%s][%d];socket Error!\n", __FILE__, __LINE__);
- if (nRetryFlag++ == 5)
- close(socket_id);
- if (errno == ECONNREFUSED)
- {
- printf("\nCannot connect port %d:", Port);
- perror("");
- sleep(30);
- continue;
- }
- printf("Connect %s port %d error!\n",
- Ip_addr, Port);
-
- }
- break;
- }
- printf("第%d个进程第%d次成功连接\n",i+1,times+1);
- memset(sDataName,0,sizeof(sDataName));
- //sprintf(sDataName,"file/test0.cfg");
- sprintf(sDataName,"file/test%d.cfg",i);
- if (ReadPETestData(sDataName,sSendName,times) != 0)
- {
- printf("[%s][%d];socket Error!\n", __FILE__, __LINE__);
- if (nRetryFlag++ == 5)
- return;
- }
- /* 读测试数据 over*/
- memset(MsgOut,0x00,sizeof(MsgOut));
- nMsgLen = getSendData(MsgOut);
- int ia = 0;
- /*
- while(ia != nMsgLen)
- {
- printf("%c",*(MsgOut+(ia++)) );
- }
- */
-
-
- nRetVal = WriteSocket( socket_id, MsgOut, nMsgLen);
- if (nRetVal <=0)
- {
- printf("Write socket call!\n");
- close(socket_id);
- /* return; */
- }
- printf("第%d个进程第%d次发送成功\n",i+1,times+1);
- alarm(20);
- memset(sNetMsgBuf, 0, sizeof(sNetMsgBuf));
- nMsgLen=ReadSocket(socket_id,sNetMsgBuf,637);
- if (nMsgLen <=0)
- {
- alarm(0);
- if (nMsgLen == E_SBREAK)
- {
- printf("[%s][%d];socket Error!\n", __FILE__, __LINE__);
- printf("errno:[%d]\n",errno);
- printf("read socket call!\n");
- close(socket_id);
- exit(1);
- }
- else
- {
- printf("errno:[%d]\n",errno);
- return ;
- }
- }
- else
- {
- }
- printf("第%d进程第%d次读取成功\n",i+1,times+1);
- alarm(0);
- /*解包,取出39域并打印*/
- memset(rspCode,0,3);
- Unpack8583(&sNetMsgBuf[11],128,rspCode);
- if(memcmp("00",rspCode,2)==0)
- {
- totle_r++;
- }
- else
- {
- totle_w++;
- }
- close(socket_id);
- }
- printf("第%d个进程执行到这里了\n",i+1);
-
- printf("\n终端号0000000%d 一共[%d]笔,成功了[%d],失败[%d]\n",i+1,times,totle_r,totle_w);
-
- //close(socket_id);
- exit(0);
- }
- if(pid<0)
- {
- printf("第%d次创建进程失败\n",i);
- }
- else{
- //printf("[%s][%d];socket Error!\n", __FILE__, __LINE__);
- }
- }
- }
复制代码 |
|