Packages fell if I do not take an active part in DatagramSocket?

I have visited so many websites and tutorials (and documents), but I have one question that remains unanswered. What happens if a UDP packet arrives when I am not currently running socket.receive (...)? Will it somehow be buffered until I call socket.receive (...) or is it completely lost?

In addition, the Android DatagramSocket implementation is thread-safe in meaning, which you can write to the socket when listening or not?

+6
source share
1 answer

The kernel network stack contains a receive buffer for each socket. You can control its size using the SO_RCVBUF setsockopt(2) option (you need to find out that there is a Java API for it).

Thus, incoming packets are queued in the kernel if there is enough space in this buffer. This is not required for the application to perform the β€œreceive” function at this particular moment. If the application does not de-energize the packets quickly enough (for example, reading), a buffer overflow occurs and the kernel starts to drop packets. On Linux (and usually Unix), you can see the number of packets dropped by using the netstat -s command.

Let me also note that this buffer is not the only place where packages can be removed. The hardware can drop packets if there is not enough space in the embedded NIC's memory. The device driver can then drop packets if the kernel is too busy and does not handle its general network input queue. You need serious messages to make this happen.

TCP tries to get around all this with full sequencing and, if necessary, retransmission. UDP, the bare-bone send and forget, does nothing about lost packets (unless you receive an error from the second send(2) on a connected UDP socket and only if ICMP works correctly.

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

+9
source

Source: https://habr.com/ru/post/894752/


All Articles