0
votes

I created a java server that listens on a specific port for udp packets, it creates a new thread with a new datagramsocket bound to that port when a packet is received, so if it gets 10 packets on that port it creates 10 threads with new sockets bound to the same port.

The problem is that although new threads and sockets are created, only the first socket created receives all the traffic, the other threads/sockets keep listening without getting any traffic.

That single thread alone doesnt keep up with the traffic and some packets are lost

Im testing this with address localhost and jmeter udp plugin

1
For tcp i know that you cannot bind multiple sockets to a port. I doubt that udp is different in that regard. Are you sure you're not getting a BindException for all Sockets except the first? - f1sh
@f1sh I set reuseaddress true to bind them to the same port, no exceptions thrown. Also checking getReuseAddress() returns true so its allowed on my system - user10156126
When using UDP, you need to have ONE socket (on one thread) receiving data as fast as possible (to reduce risk of loosing datagrams) and putting the received data into some queue for further processing (in other thread(s)). - C. Gonzalez
If you want TCP, use TCP. - user207421

1 Answers

0
votes

A datagram socket is like a server socket; only one bound instance per host.

UDP is lossy by design (by the nw and by the slow destinations not reading fast enough) but with a perfect app you can expect MB/s on localhost, perhaps even on LAN, but not that fast on WAN or with a limited app.

Most UDP apps will have a fast listener to just avoid os level losses from overflows (no java control over buffer space there) and COPY (not share the byte array, be careful) the datagram to queues in the app which can be tuned for depth at will, and a pool of worker threads taking from such queue.