w***@gmail.com
2005-12-01 20:41:36 UTC
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
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