tags:

views:

41

answers:

2

Assume Linux and UDP is used.

The manpage of recvfrom says:

The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested.

If this is the case, then it is highly possible to return partial application level protocol data from the socket, even if desired MAX_SIZE is set.

Should a subsequent call to recvfrom be made?

In another sense, it's also possible to have more than the data I want, such as two UDP packets in the socket's buffer. If a recvfrom() is called in this case, would it return both of them(assume within MAX_SIZE)?

I suppose there should be some application protocol level size info at the start of each UDP msg so that it won't mess up.

+1  A: 

I think the man page you want is this one. It states that the extra data will be discarded. If there are two packets, the recvfrom call will only retrieve data from the first one.

Mark Wilkins
then what about subsequent recvfrom() call? Will it get the second one which already exists in the buffer left by the first one?
Figo
@Figo: Yes, it returns one at a time.
Mark Wilkins
A: 

Well..I got a better answer after searching the web:

Don't be afraid of using a big buffer and specifying a big datagram size when reading... recv() will only read ONE datagram *even if there are many of them in the receive buffer* and they all fit into your buffer... remember, UDP is datagram oriented, all operations are on those packets, not on bytes...

A different scenario would be faced if you used TCP sockets.... TCP doesn't have any boundary "concept", so you just read as many bytes as you want and recv() will return a number of bytes equals to MIN(bytes_in_buffer, bytes_solicited_by_your_call)

REF: http://www.developerweb.net/forum/archive/index.php/t-3396.html

Figo