- 论坛徽章:
- 0
|
#include <stdio.h>
#include <string.h>
#include "mysock.h"
#include<time.h>
#include <sys/timeb.h>
#include <sys/time.h>
#include "unixsocket.h"
#define MILLION 1000000
void * doit(void *arg)
{
struct timeval tpstart;
struct timeval tpend;
long timerecv;
int nNum = 0;
int bFlg;
char szBuff[512] = {0};
int iCn = 0;
char szUrl[2048] = {0};
char szIP[32] = {0};
char szPort[16] = {0};
int nSocket = -1;
int nRet = 0;
char szHost[512] = {0};
char szRevBuff[1024] = {0};
int nLen = 0;
char myResponse[2048] = {0};
char szGet[1024*4] = {0};
//int aa = *(int *)arg;
int aa = 0;
printf("<%d>\n", aa);
sleep(2);
printf("test is start<%d> \n", aa);
strcpy(szIP,"192.168.3.220");
strcpy(szPort,"6999");
printf("IP<%s>\n",szIP);
printf("PORT<%s>\n",szPort);
int one= time((time_t*)NULL);
char szTem[8] = {0};
gettimeofday(&tpstart,NULL);
int bb = aa+20000;
/*
while (!mysock.Connect(szIP, atoi(szPort)))
{
usleep(100);
printf("wait...\n");
}*/
while(aa < 1000)
{
/* while ((nSocket = connect_to(szIP, atoi(szPort))) == -1)
{
usleep(100);
printf("wait...\n");
}*/
char szSendBuff[1024] = {0};
CUnixSocket mysock;
mysock.SetTimeOut(10*1000);
while (!mysock.Connect(szIP, atoi(szPort)))
{
usleep(100);
printf("wait...\n");
}
int g=0;
szSendBuff[11] = '\0';
char ss[10] = {0};
int nlen = 0;
memset(szSendBuff, 0, sizeof(szSendBuff));
sprintf(szSendBuff, "%011d", aa);
printf("begin to safe_send()<%d>\n",aa);
//if (safe_send(nSocket, szSendBuff, sizeof(szSendBuff), 3) == sizeof(szSendBuff))
if(mysock.SendMsg(szSendBuff, strlen(szSendBuff)))
{
printf("safe_send: send<%s>\n",szSendBuff);
memset(ss, 0, sizeof(ss));
//nlen = safe_recv(nSocket, ss, 1, 3);
nlen = mysock.Receive(ss, 1);
if (nlen != 1)
{
printf("safe_recv: error<%d>\n", nlen);
}
else
{
printf("safe_recv: recv<%s> len<%d>\n\n", ss, nlen);
}
usleep(1000);
}
else
{
printf("send error\n");
}
// shutdown(nSocket, 2);
// close(nSocket);
mysock.Close();
aa++;
sleep(1);
}
printf("<%d>\n", aa);
//mysock.Close();
gettimeofday(&tpend, NULL);
int two= time((time_t*)NULL);
timerecv = MILLION*(tpend.tv_sec - tpstart.tv_sec)+ tpend.tv_usec - tpstart.tv_usec;
printf("<%ld>\n",timerecv);
printf("one<%d>two<%d>time<%d>\n",one,two,two-one);
}
int main(int argc, char *argv[])
{
pthread_t tid[20];
int i = 0;
int j = 0;
for(i ; i<5; i++)
{
if (pthread_create(&tid,NULL,doit,NULL)!=0)
{
printf("pthread_create is error[%d][%d]\n",i, j);
}
else
{
printf("pthread_create is ok[%d][%d]\n",i,j);
}
j = j+ 250000;
}
for (i =0; i<5; i++)
{
pthread_join(tid,NULL);
}
return 0;
} |
|