Search code examples
ctcpudppartial

Is this the correct way to handle partial reads of UDP messages?


So in the case of TCP streams, one needs to keep track of how much is read of the message by each recv call. Partial reads can then be pieced together and ultimately the message can be received ok.

But for UDP messages, how should partial reads be handled (assuming that all UDP messages are small enough to avoid fragmentation)? As remaining data of partial messages seem to be discarded, is it just a matter of making sure that recvfrom returns the same size as the buffer that's been sent? If there is a difference it means it's a partial and defective message and it should be skipped.

So conceptually, while the TCP example needs a loop, the UDP example just needs an if statement.

Is this correct?


Solution

  • correct. However that if condition will work only if receiver knows in advance how many bytes are being sent by the sender in advance.