开发者

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

I've visited so many websites a开发者_StackOverflownd 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜