收到的数据多于收到的流量发送数据

时间:2014-12-08 10:27:30

标签: c sockets

我正在开发流量生成。 运行Sender / Receiver之后,它发现我收到的数据多于发送到收到的数据。 我哪里错了?定时器也不会在精确循环时间停止?发件人提前停止,接收器稍后停止。

/ *

gcc CGenS.c -o Server gcc CGenS.c -Recv

./ Server -m 1 -s localhost -p 5000 -z 256 -T 10000 -P t ./Recv -m 0 -p 5000 -z 256 -P t

问题:

* /

if(mode==1)    
{
    serv_addr.sin_family = AF_INET;      
    bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr, server->h_length);             
    serv_addr.sin_port = htons(portno);          
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)           
        error("ERROR connecting");              

        for (i=0;i<packet_size;i++)
            buffer[i]='A';

        gettimeofday(&tv1, NULL);

do 
{

//#pragma omp parallel private(nthreads, tid)
 //{

  /* Obtain thread number */
 // tid = omp_get_thread_num();
  // if (tid == 0) 

   // {
   // nthreads = omp_get_num_threads();
    //printf("Number of threads = %d\n", nthreads);
   // }*/
    n = write(sockfd,buffer,strlen(buffer));
    num_pkt_send++;
    TotalSend=TotalSend+packet_size;
    if (n < 0) 
    {
         error("ERROR writing to socket");
          exit(1);
    }
    printf("%d Bytes Send: %d\n",num_pkt_send,sizeof buffer);
    gettimeofday(&tv2, NULL);
    //}
} while((tv2.tv_sec-tv1.tv_sec)!=Loop_time);
  printf ("Total Sending time = %f seconds\n",(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 + (double) (tv2.tv_sec - tv1.tv_sec));
 printf("Total Bytes Send: %d\n",TotalSend);
 printf("Total Packet Send: %d\n",num_pkt_send);
    close(sockfd);
    }

else if (mode==0)
{

     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);

 if (bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr))< 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);

     newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0)      
     error("ERROR on accept");
     bzero(buffer,packet_size);
     gettimeofday(&tv1, NULL);
while(n>0)
{
     n = read(newsockfd,buffer,packet_size);


     TotalRecv=TotalRecv+packet_size;
     num_pkt_rec++;

     printf("%d Bytes Received: %d\n",num_pkt_rec ,sizeof buffer);


}
gettimeofday(&tv2, NULL);
printf ("Total Receiving time = %f seconds\n",(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 + (double) (tv2.tv_sec - tv1.tv_sec));
 printf("Total Packer Received: %d\n",num_pkt_rec);
 printf("Total Bytes Received: %d\n",TotalRecv);
     close(newsockfd);
     close(sockfd);

}
return 0; 
}

0 个答案:

没有答案