Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are packets dropped if I am not actively receiving from a DatagramSocket?

I've visited so many websites and tutorials (and docs), but I still have one question unanswered. What happens, if UDP packet arrives, when I'm not currently running socket.receive(...)? Will it buffer somehow until I invoke socket.receive(...) or is it completely lost?

Also is Android's DatagramSocket implementation thread-safe in meaning, that you can write to socket when listening to it, or not?

like image 710
LordTwaroog Avatar asked Aug 10 '11 19:08

LordTwaroog


1 Answers

The kernel network stack keeps a receive buffer per socket. You have control over the size of it with SO_RCVBUF option to the setsockopt(2) system call (you'll have to figure out what's the Java API for it).

So the incoming packets are queued in the kernel as long as there's enough space in that buffer. It's not required for the application to be executing the "receive" function at that particular moment. If the application is not de-queuing (i.e. reading) the packets fast enough, the buffer overflows and kernel starts dropping the packets. On Linux (and generally Unix) you should be able to see the packet drop count with netstat -s command.

Let me also note that this buffer is not the only place where packets can get dropped. The hardware might drop packets if there's not enough space in its on-NIC memory. Then the device driver can drop packets if the kernel is too busy and not processing its general network input queue. You need serious message rates for this to happed though.

TCP tries to get around all this with total sequencing and, if necessary, retransmission. UDP, being bare-bones "send and forget", does not do anything about dropped packets for you (except in corner cases where you get an error from the second send(2) on a connected UDP socket and only if ICMP works properly).

The bottom line is that packets are buffered, but up to the point, then discarded.

like image 181
Nikolai Fetissov Avatar answered Oct 20 '22 13:10

Nikolai Fetissov