Discussion:
recvfrom and recv socket buffer
(too old to reply)
w***@gmail.com
2005-12-01 20:41:36 UTC
Permalink
I am writing a communication program using both TCP and UDP, for two
different types of communication. The UDP server application is for
constant communication with another component and the TCP is for
sporadic communication with another component. So far I have the TCP
communication running well. In that code, I make this call:

if ((nRead = recv(socket, &buffer[0], 1, 0)) >= 0)

to read the first byte. I can continue to call this function to read
partial bytes of the buffer until the end of the buffer is reached,
without any more packets being sent. So if 20 bytes are sent in one
packet, for example, I can call recv 20 times in a loop, reading in a
byte at a time, until recv will finally block, on the 21st read. When I
use TCP and recv, the rest of the sent data is still left in the socket
buffer.

However, when I used UDP and call the recvfrom/recv function, it
removes the whole packet/message from the socket buffer and subsequent
calls to recvfrom/recv before a new packet arrives are blocked.

I've tried to use recvfrom with the MSG_PEEK sockopt, but this won't
help in case of processing partial packets. At least, without making my
code significantly more intricate and convoluted.

I've also tried to connect on the UDP socket but I get the same
results.

Is this a limitation to UDP, or is there a workaround? It seems that
TCP is more analogous to a serial connection where the socket buffer is
not affected by partial recv commands.

Thanks in advance,
Walter Shirey
Martin Raabe
2005-12-02 07:40:25 UTC
Permalink
Hello Walter,
***@gmail.com schrieb:
[snip]
Post by w***@gmail.com
Is this a limitation to UDP, or is there a workaround? It seems that
TCP is more analogous to a serial connection where the socket buffer is
not affected by partial recv commands.
Yes, you are right, it is the way you have to handle UDP.

On TCP you can expect the datastream to be handled by the TCP protocol.
UDP says, there has one message arrived and you are invited to get it or
not. If you decide to go for it, take it all!
Usually the application knows the mximum amount of bytes to be
transferred and so the application has to read ALL bytes from the UDP
socket.
Then an application function may parse the buffer byte by byte.

So you have found outcorrectly, what the recv() difference is between
TCP and UDP sockets.

Hope it helps.
--
BaSystem Martin Raabe
E: Martin.Raabe<at>B-a-S-y-s-t-e-m<dot>de
w***@gmail.com
2005-12-05 14:37:40 UTC
Permalink
Thanks for your help, Martin. If this is the case, then why does the
VxWorks manual boast the ability to store 41600 bytes in the UDP
buffer, allowing it to store up to 40 messages if they can only be
processed one at a time? Is this 40 messages only useful when having 40
tasks recv UDP buffers at once?

Thanks again,
Walter Shirey

Loading...