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?

Upvotes: 1

Views: 307

Answers (1)

jedwards
jedwards

Reputation: 30250

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.

Upvotes: 3

Related Questions