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?
It is not possible to do partial reads in UDP. UDP guarantees the datagram received is as was sent, fragmented or not, so must have received the entire datagram before you can read it.
See: http://en.wikipedia.org/wiki/User_Datagram_Protocol, you can follow the references in there for more official sources.
As for reading into a buffer you will need a buffer at least the size of any possible datagram. Or another common way is to include the size on the datagram near the beginning of the datagram so you can read only those bytes to get that number then use a buffer the appropriate size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With