title: Receiving UDP in Java without dropping packets
body: <p>I have a library which I need to improve since it is dropping to many packets. I want to receive a RTP-Stream, but the streamer is sending bursts of 30-40 packets within a millisecond (MJPEG-Stream). I can see the packets being complete when monitoring traffic in Wireshark. But when trying to receive them in Java, I lose a lot of those packets.</p>
  
  <p>I have already been able to improve the libraries behavior by implementing a ring buffer that would constantly get filled whenever a packet is available and a separate reader thread that reads from this buffer. But I'm still not able to get all the packets from my socket that I can see in wireshark. Through RTP sequence numbers I can monitor in the reader thread if the packet processed is the one expected.</p>
  
  <p>The following code is handling packet receiving:</p>
  
  <pre><code>private volatile byte[][] packetBuffer = new byte[1500][BUFFER_SIZE];
  private volatile DatagramPacket[] packets = new DatagramPacket[BUFFER_SIZE];
  private volatile int writePointer = 0;
  
  public void run() {
      Thread reader = new RTPReaderThread();
      reader.start();
  
      while (!rtpSession.endSession) {
  
          // Prepare a packet
          packetBuffer[writePointer] = new byte[1500];
          DatagramPacket packet = new DatagramPacket(packetBuffer[writePointer], packetBuffer[writePointer].length);
  
          // Wait for it to arrive
          if (!rtpSession.mcSession) {
              // Unicast
              try {
                  rtpSession.rtpSock.receive(packet);
              } catch (IOException e) {
                  if (!rtpSession.endSession) {
                      e.printStackTrace();
                  } else {
                      continue;
                  }
              }
          } else {
              // Multicast
              try {
                  rtpSession.rtpMCSock.receive(packet);
              } catch (IOException e) {
                  if (!rtpSession.endSession) {
                      e.printStackTrace();
                  } else {
                      continue;
                  }
              }
          }
          packets[writePointer] = packet;
  
          this.incrementWritePointer();           
          synchronized (reader) {
              reader.notify();
          }
      }
  }
  </code></pre>
  
  <p>What I already know:</p>
  
  <ul>
  <li>I know that UDP is allowed to lose packets, but I still want to
  achieve the best possible result. If wireshark can see the packet, I
  want to be able to retrieve it as well, if possible.</li>
  <li>I know that the ring buffer is never full while losing packets, so
  this doesn't make me lose packets either. I tried with BUFFER_SIZES
  of 100 and even 1000, but I already lose the first packets before a
  total of 1000 packets has been send.</li>
  </ul>
  
  <p>So the question is: what is best practice to receive as many packets as possible from a DatagramSocket? Can I improve handling of packet bursts?</p>