title: What would cause UDP packets to be dropped when being sent to localhost?
body: <p>I'm sending very large (64000 bytes) datagrams. I realize that the MTU is much smaller than 64000 bytes (a typical value is around 1500 bytes, from my reading), but I would suspect that one of two things would happen - either no datagrams would make it through (everything greater than 1500 bytes would get silently dropped or cause an error/exception to be thrown) or the 64000 byte datagrams would get chunked into about 43 1500 byte messages and transmitted transparently.</p>
  
  <p>Over a long run (2000+ 64000 byte datagrams), about 1% (which seems abnormally high for even a LAN) of the datagrams get dropped. I might expect this over a network, where datagrams can arrive out of order, get dropped, filtered, and so on. However, I did not expect this when running on localhost.</p>
  
  <p>What is causing the inability to send/receive data locally? I realize UDP is unreliable, but I didn't expect it to be so unreliable on localhost. I'm wondering if it's just a timing issue since both the sending and receiving components are on the same machine.</p>
  
  <p>For completeness, I've included the code to send/receive datagrams.</p>
  
  <p>Sending:</p>
  
  <pre><code>DatagramSocket socket = new DatagramSocket(senderPort);
  
  int valueToSend = 0;
  
  while (valueToSend &lt; valuesToSend || valuesToSend == -1) {
      byte[] intBytes = intToBytes(valueToSend);
  
      byte[] buffer = new byte[bufferSize - 4];
  
       //this makes sure that the data is put into an array of the size we want to send
      byte[] bytesToSend = concatAll(intBytes, buffer);
  
      System.out.println("Sending " + valueToSend + " as " + bytesToSend.length + " bytes");
  
      DatagramPacket packet = new DatagramPacket(bytesToSend,
                          bufferSize, receiverAddress, receiverPort);
  
      socket.send(packet);
  
      Thread.sleep(delay);
  
      valueToSend++;
  }
  </code></pre>
  
  <p>Receiving:</p>
  
  <pre><code>DatagramSocket socket = new DatagramSocket(receiverPort);
  
  while (true) {
      DatagramPacket packet = new DatagramPacket(
              new byte[bufferSize], bufferSize);
  
      System.out.println("Waiting for datagram...");
      socket.receive(packet);
  
      int receivedValue = bytesToInt(packet.getData(), 0);
  
      System.out.println("Received: " + receivedValue
              + ". Expected: " + expectedValue);
  
      if (receivedValue == expectedValue) {
          receivedDatagrams++;
          totalDatagrams++;
      }
      else {
          droppedDatagrams++;
          totalDatagrams++;
      }
  
      expectedValue = receivedValue + 1;
      System.out.println("Expected Datagrams: " + totalDatagrams);
      System.out.println("Received Datagrams: " + receivedDatagrams);
      System.out.println("Dropped Datagrams: " + droppedDatagrams);
      System.out.println("Received: "
              + ((double) receivedDatagrams / totalDatagrams));
      System.out.println("Dropped: "
              + ((double) droppedDatagrams / totalDatagrams));
      System.out.println();
  }
  </code></pre>