Thread: More Packet is Received Than Threaded Sender Sent

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Mumbai, Maharashtra, India, India
    Posts
    14

    More Packet is Received Than Threaded Sender Sent

    Questions :
    I am running a client Server Program whose Sender and Receiver loop is given below .I have multi threaded Sender using OMP .As per My logic Receive should receive same amount of data send by Sender ,But its receiving more packet than Sender sent .I am not able to identify the Bug !!

    Code:
    /* 
    Running As Follow : 
                gcc UTGen.c -o Sender
                gcc UTGen.c -o Receiver
    
                 ./Receiver -m 0 -p 5000 -z 256 -P t
    
                  export OMP_NUM_THREADS=4
                ./Sender -m 1 -s localhost -p 5000 -z 256 -T 10 -P t             
                */
    
    
          // Sender Code
    
                    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);
        }
        #pragma omp critical
        {
        n = write(sockfd,buffer,packet_size);
        num_pkt_send++;
        TotalSend=TotalSend+n;
        }
        if (n < 0) 
        {
             error("ERROR writing to socket");
              exit(1);
        }
        printf("%d Bytes Send: %d\n",num_pkt_send,n);
        gettimeofday(&tv2, NULL);
        }
    } while((tv2.tv_sec-tv1.tv_sec)<=Sen_Loop_time);
    
          // Receiver Code
    
                 gettimeofday(&tv1, NULL);
    
            while(n>0)
            {
                 n = read(newsockfd,buffer,packet_size);
                 TotalRecv=TotalRecv+n;
                 num_pkt_rec++;
                 printf("%d Bytes Received: %d\n",num_pkt_rec ,n);       
    
            }
            gettimeofday(&tv2, NULL);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Neither send nor receive are guaranteed to preserve whatever framing you had in mind.

    All of these are completely valid for a stream protocol.

    Send: "hello"
    Recv: "hel" then later "lo"

    Send: "h" then on a retry "ello" (you have to do the retry yourself, check the return result of send)
    Recv: "he" then "llo"

    Send: "h" "e" "l" "l" "o"
    Recv: "hello"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Location
    Mumbai, Maharashtra, India, India
    Posts
    14
    Are you trying to Say that ..Increased Data is due to Packet Overhead ....
    which happen due to fragmentation of data ...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    n = write(sockfd,buffer,packet_size);
    n = read(newsockfd,buffer,packet_size);

    Why don't you just add some code, and print a message each time n != packet_size is true.

    fragmentation can happen any time you try to send/recv more than 1 byte.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help c++ http sender
    By Anddos in forum C++ Programming
    Replies: 0
    Last Post: 03-28-2010, 11:37 PM
  2. Going from single-threaded to multi-threaded
    By Dino in forum C Programming
    Replies: 11
    Last Post: 03-23-2008, 01:14 PM
  3. about *Sender
    By sawer in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2006, 08:52 AM
  4. sender source problem
    By Sniper in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 11:33 AM
  5. TCP/IP Packet Sender/Receiver
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 02-23-2002, 09:57 AM

Tags for this Thread