title: Some UDP packets don't arrive to the destination C++
body: <p>I have to test the TCP and UDP connection between two Amazon EC2 instances. For the moment I'm trying to test it from one instance to itself, this is, in localhost. I've written a C++ program to do that.
  It works perfectly with TCP, and it reachs from 600-900 MB/s, which I think its too high but I also think my calculations are correct.
  The problem is in UDP tests. 
  My client sends packets of 1024 B until it has sent 256 KB and my server waits for packets until it has received 256 KB. I have to measure the time to receive 256 KB so I do this 10 times in order to get the average time. This is a fragment of code:</p>
  
  <p><strong>Server</strong></p>
  
  <pre><code>    char buffer[256*1024];
      memset(&amp;buffer, 0, sizeof buffer);
      for (int i = 0; i &lt; 10; i++) {
          int rcvd = 0;
          while(rcvd &lt; 256*1024) {
              rcvd += recvfrom(socket, (void *) &amp;buffer, sizeof(buffer), 0, &amp;addr, &amp;fromlen);
          }
          printf("rcvd = %d\n", rcvd);
      }
  </code></pre>
  
  <p><strong>Client</strong></p>
  
  <pre><code>    char buffer[1024];
      memset(&amp;buffer, 1, sizeof buffer);
      for (int i = 0; i &lt; 10; i++) {
          int sent = 0;
          while(sent &lt; 256*1024) {
              sent += sendto(sock, buffer, sizeof (&amp;buffer), 0, (struct sockaddr *)&amp;server, slen);
          }
          cout &lt;&lt; sent &lt;&lt; " Bytes sent." &lt;&lt; endl;     
      }
  </code></pre>
  
  <p>The client sends a packet of 1024 Bytes until it has sent 256 KB, in each iteration; and the server waits for a packet of 256 KB in each iteration. 
  It seems the server is getting approximately 180000 Bytes (256 KB = 262144 Bytes) in the last iteration so some packets are lost in the way.
  Any idea of why is this happening?
  Thanks!</p>
  
  <p><strong>Edited:</strong></p>
  
  <p>I tried to detect any error as <strong>Martin James</strong> suggested. The code is now like:</p>
  
  <p><strong>Server</strong></p>
  
  <pre><code>    for (int i = 0; i &lt; 10; i++) {
      timestamp_t ini = get_timestamp();
      int rcvd = 0;
      int res = 0;
      while(rcvd &lt; 256*1024) {
          rcvd += recvfrom(socket, (void *) &amp;buffer, sizeof(buffer), 0, &amp;addr, &amp;fromlen);
          if (res == -1 ){
              printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
              sleep(5);
          }
          else{
              rcvd += res;
          }
      }
  </code></pre>
  
  <p><strong>Client</strong></p>
  
  <pre><code>for (int i = 0; i &lt; 10; i++) {
          int sent = 0;
          int res = 0;
          while(sent &lt; 256*1024) {
              res = sendto(sock, buffer, sizeof (&amp;buffer), 0, (struct sockaddr *)&amp;server, slen);
              if (res == -1 ){
                  printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
                  sleep(5);
              }
              else{
                  sent += res;
              }
          }
          cout &lt;&lt; sent &lt;&lt; " Bytes sent." &lt;&lt; endl;
  
      }
  </code></pre>
  
  <p>However, no error was detected in any of the attempts.
  The first three tests were successful. This is, Client sent 256 KB 10 times and Server received those 256 KB 10 times. Then, on the fourth attempt, the Server didn't receive all the data. I tried again, and I though maybe the pipe was kind of collapsed in that port. So then I changed the port several times but the Server didn't receive the whole data anymore, and therefore it got stuck on the while loop waiting to complete his final 256 KB block.
  UDP packets lost from localhost to localhost? Is that the reason?
  Thank you all for the responses.</p>