// JP opened flex table

Click to See Complete Forum and Search --> : What happens to UDP packets sent to a starting-up host?


Lindley
November 25th, 2007, 03:25 PM
Let's say I have a process which will eventually listen for UDP packets on localhost port 10000, and another which is immediately sending UDP packets to localhost port 10000.

If the listening process finishes starting up before the first packet arrives, obviously it will be received normally. But what if the packet arrives first? Is it possible that a socket created later on that port could still receive it, or would the act of binding the socket clear whatever buffer it may have been cached in?

Doron Moraz
November 25th, 2007, 07:21 PM
The packets are NOT cached anywhere.

If your server is not listening for connections, and someone send you a UDP packet, the following steps will occur.

1. The OS will check if the application is listening at that port;
2. No application listens at that port, drop the packet;
3. Reply with an ICMP Destination Unreachable packet.

Regards
Doron Moraz

Lindley
November 26th, 2007, 11:13 AM
Okay, so what if sendto is called a few moments before a recvfrom? It won't be dropped then, will it, since there is a socket bound to the port?

MikeAThon
November 26th, 2007, 11:54 AM
Without an active call to recvfrom, the Winsock stack will not have a buffer in which it can store the sent-to message. The packet will be dropped, as explained by Doron above.

Mike

Lindley
November 26th, 2007, 02:38 PM
I see. I'm using Unix sockets, not Winsock, but I assume the same principal applies.

That's actually fairly annoying. Should the recvfrom then be done constantly, spawning new threads to actually handle the datagrams as they arrive? Assuming they could come from multiple hosts (the entire reason for using a connectionless protocol in the first place).

Edders
November 27th, 2007, 07:26 AM
You can bind() your socket to a particular port and then call recv() to receive the datagrams. With the call to bind() you (kind of) register your application as being interested in packets for that socket. The OS will then buffer these for you. You can get these buffered packets by calling recv().

MikeAThon
November 27th, 2007, 11:01 AM
Without an active call to recvfrom, the Winsock stack will not have a buffer in which it can store the sent-to message. The packet will be dropped, as explained by Doron above.
The more I think about it, the more I'm convinced that this advice is not correct. The call to bind() should be enough. Ther should not need to be an active call to recvfrom(). Unless your UDP datagrams are very large, the stack should store at least one datagram until a call to recvfrom(). Otherwise, the whole concept of blocking (and non-blocking with WSAEWOULDBLOCK as a returned error) would not make sense.

Sorry for the false direction.

Mike

Lindley
November 27th, 2007, 05:51 PM
I had assumed bind was a requirement, since I see no other way to associate a connectionless socket with a given port.

//JP added flex table