简体   繁体   中英

How does Java handle multiple UDP packets receipt?

Imagine the following scenario in UDP packets:

  1. Server broadcasts "Hello" message and waits for answers
  2. Client 1 answers with "Hi"
  3. Client 2 also answers with "Hi"

Now, I'm interested to what happens with the server if the second answer is received before the server is finished processing the first answer.

On the one hand, UDP is by design unreliable, and since I haven't called datagramSocket.receive(myPacket) for the second time, I assume the second answer will be silently discarded.

On the other hand, the socket is still open, so maybe Java (or even a lower level, like in the kernel network stack) will queue the packet.

What would really happen in this case?

The OS's network stack buffers the Datagram.

As long as the socket remains open, additional calls to .receive() will fetch the datagram contents.

If the socket is closed while packets are buffered, then they're silently discarded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM