Why doesn't the recv
system call just block until all the data is received? Every time I have seen a recv
call, it's in a while loop which just keeps on calling recv
until all the data is there. Why not just have recv
block in the first place?
You can request that recv block until all data is received, with the MSG_WAITALL
flag. However, if a signal arrives, a system call that has performed some work (ie, receiving part of the data) cannot be automatically restarted to receive the rest. As such, even with MSG_WAITALL
, there are cases where the recv call may return before the buffer is full, and you must be prepared to handle these cases. Given this, many people simply choose to loop and not bother with little-known flags such as MSG_WAITALL
.
As for why this is the case by default, there are a few reasons that come to mind:
telnet
protocol, which was popular around the time when the BSD sockets API was designed. You'll typically be receiving a handful of bytes at a time, there are no length fields telling you how much data to expect, and moreover you need to display that data right away. It doesn't make sense to block until you fill a buffer here. Likewise with line-oriented protocols such as SMTP or IMAP - you don't know how long the command is until you've received all of it.recv
is often used for datagram sockets, where it receives a single datagram, even if it's much smaller than the buffer provided. The natural extension to streaming sockets is to just return as much as you can without waiting.But most importantly, since you need to be prepared to deal with a partial buffer anyway, it's good to force people to deal with it by default, so they turn up the bugs in their loop early - rather than having them remain hiding until a signal arrives at an unfortunate moment.
In most cases, you don't know how much data is "all of the data". For example, if you're receiving data in a line-oriented protocol, a line might be 10 bytes long or 65 bytes long.
You can change socket flags to either blocking or non-blocking. Your specific case actually has nothing to do with blocking or not blocking.
It would make no sense to make a network function behave in the way you describe by default - what if the stream never ends .. should the program never end? Prima facia, this doesn't seem like healthy default behavior.
Read http://www.scottklement.com/rpg/socktut/nonblocking.html to familiarize yourself with blocking and non-blocking IO.
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