0

I am using UDP client to send about 20k requests per second with <1k data of every request. I need implement a UDP server via Java. coding like following:

public void startRecieve() throws IOException {
    udpSocket = new DatagramSocket(Constant.PORT);
    byte[] buffer = new byte[Constant.SIZE];
    udpPacket = new DatagramPacket(buffer, buffer.length);

    int len;
    while (true) {
        udpSocket.receive(udpPacket);
        len = udpPacket.getLength();
        if (len > 0) {
            // handing the data using other thread pool
        }
    }
}

Is there any way to make the UDP server packet loss be lower?

Thanks.

andy
  • 1,336
  • 9
  • 23
  • 1
    Install a fibre cable and insulate it to prevent noise, that should help. – We are Borg Sep 04 '15 at 07:42
  • @andy:- Check this: http://stackoverflow.com/questions/8267271/how-to-minimize-udp-packet-loss – Rahul Tripathi Sep 04 '15 at 07:46
  • @andy: What are your (both send and recv) socket buffer sizes ? Did you call the functions setReceiveBufferSize and setSendBufferSize on both sides ? – Malkocoglu Sep 04 '15 at 07:52
  • You should use already existing UDP server, processors available readily rather than constructing from scratch. Secondly, UDP is not reliable, if you want something reliable, go for TCP, want something real-time, go for RTP. But don't expect that by posting 10 lines of code, you would expect users to help on how lower UDP error rate. Also, check the question Rahul has put, the answer in that question is on point. – We are Borg Sep 04 '15 at 07:54
  • See also: https://stackoverflow.com/a/7968907/59087 – Dave Jarvis Jan 10 '20 at 16:32

1 Answers1

2

The packet loss is in the network, the only programming options are to

  • send less data e.g. compress it or
  • resend data on a loss so that less data is lost.
  • use TCP which handles packet loss for you.
  • use a library like Aeron which uses UDP and handles packet loss for you.

The best solution is almost always to fix the network to reduce the loss in the first place, but have a strategy which accepts some loss will happen.

Note: as UDP is a lossy protocol but TCP is not, many routers are optimised to drop UDP packets when the TCP load increases (as the router expects that any dropped TCP packet will just be sent again anyway)

This can mean that under load, you can see higher packet loss with UDP than TCP.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130