views:

885

answers:

2

I have a java app on linux which opens UDP socket and waits for messages.

After couple of hours under heavy load, there is a packet loss, i.e. the packets are received by kernel but not by my app (we see the lost packets in sniffer, we see UDP packets lost in netstat, we don't see those packets in our app logs).

We tried enlarging socket buffers but this didnt help - we started losing packets later then before, but that's it.

For debugging, I want to know how full the OS udp buffer is, at any given moment. Googled, but didn't find anything. Can you help me?

P.S. Guys, I'm aware that UDP is unreliable. However - my computer receives all UDP messages, while my app is unable to consume some of them. I want to optimize my app to the max, that's the reason for the question. Thanks.

+4  A: 

You are trying to solve the wrong problem. UDP is unreliable communication, period. If packet loss is a problem for you, you should either implement your own retransmission/error control algorithm or not using UDP in the first place. Drop it entirely in favor of TCP, or perhaps something more advanced like SCTP or even DCCP.

UDP is datagram-based, socket buffers should be bigger than the maximum length of an UDP datagram that your application may receive, up to 64kiB. If your application may transmit datagrams larger than this, then it is another reason you should not be using UDP. And it really doesn't matter how big is the buffer, you still may lose packets if your application can't read the socket faster than packets arrive.

You say that you want to know how full the UDP buffer is. This is the sort of thing that doesn't really matter. Just read everything that is waiting on the buffer and you can be sure that when it blocks the buffer will be empty.

If you still want to know how full the buffer is, read the file /proc/net/upd, column rx_queue. But if you see any value different than zero in that column, it just means that your application is not reading the socket fast enough.

Juliano
Thanks for the rx_queue, for the rest - see update)
Yoni Roit
+1  A: 

As Juliano already told, it seems you are solving the wrong problem.

Reliable User Datagram Protocol might be an interesting alternative, if UDP packet loss is an issue for your application.

bene
Thanks, but I don't control the protocol, it's a standard thing
Yoni Roit