At some point when coding sockets one will face the receive-family of functions (recv
, recvfrom
, recvmsg
).
This function accepts a FLAG argument, in which I see that the MSG_WAITALL
is used in many examples on the web, such as this example on UDP.
Here is a definition of the MSG_WAITALL
flag
MSG_WAITALL (since Linux 2.2)
This flag requests that the operation block until the full request is satisfied. However, the call may still return less data than requested if a signal is caught, an error or disconnect occurs, or the next data to be received is of a different type than that returned. This flag has no effect for datagram sockets.
Hence, my two questions:
- Why would one need to use
MSG_WAITALL
FLAG instead of0
FLAG? (Could someone explain a scenario of a problem for which the use of this would be the solution?) - Why to use it with UDP?
CodePudding user response:
As the quoted man page mentions, MSG_WAITALL
has no effect on UDP sockets, so there's no reason to use it there. Examples that do use it are probably confused and/or the result of several generations of cargo-cult/copy-and-paste programming. :)
For TCP, OTOH, the default behavior of recv()
is to block until at least one byte of data can be copied into the user's buffer from the sockets incoming-data-buffer. The TCP stack will try to provide as many bytes of data as it can, of course, but in a case where the socket's incoming-data-buffer contains fewer bytes of data than the user has passed in to recv()
, the TCP stack will copy as many bytes as it can, and return the byte-count indicating how many bytes it actually provided.
However, some people find would prefer to have their recv()
call keep blocking until all of the bytes in their passed-in array have been filled in, regardless of how long that might take. For those people, the MSG_WAITALL
flag provides a simple way to obtain that behavior. (The flag is not strictly necessary, since the programmer could always emulate that behavior by writing a while()
loop that calls recv()
multiple times as necessary, until all the bytes in the buffer have been populated... but it's provided as a convenience nonetheless)