简体   繁体   中英

Linux recv vs. BSD recv vs. POSIX recv return zero behavior

I've been writing a piece of network library and one thing that has been bothering me is the behavior of the recv() function, namely its possible return values.

The POSIX man page documents 3 cases:

  • Upon successful completion, recv() shall return the length of the message in bytes
  • If no messages are available to be received and the peer has performed an orderly shutdown, recv() shall return 0
  • Otherwise, -1 shall be returned and errno set to indicate the error

Notice here how the returned "length of the message" is not even certified to be above zero. The rest of the man page doesn't cover the possibility of the provided length argument being zero. The Errors section also doesn't indicate that zero is an invalid value for length .

Now, the Linux man page for recv() indicates that:

The value 0 may also be returned if the requested number of bytes to receive from a stream socket was 0.

And here comes the first BSD* man page I found, the OpenBSD man page for recv() which seems similar if not identical to others BSD* distros; and which says:

These calls return the number of bytes received, or -1 if an error occurred.

Reading the man page, it seems that the BSD implementation prefer to return an error on connection closure ( ECONNRESET ?) rather than returning zero.

My question therefore is... What in the hell is happening here? What model should I follow to make sure my library works with all recv() implementations? Is it normal that none of these implementation seem to respect the POSIX model? Is returning zero even a thing for BSD recv() ?

I've been writing a piece of network library and one thing that has been bothering me is the behavior of the recv() function, namely its possible return values.

The POSIX man page documents 3 cases:

  • Upon successful completion, recv() shall return the length of the message in bytes
  • If no messages are available to be received and the peer has performed an orderly shutdown, recv() shall return 0
  • Otherwise, -1 shall be returned and errno set to indicate the error

Notice here how the returned "length of the message" is not even certified to be above zero. The rest of the man page doesn't cover the possibility of the provided length argument being zero. The Errors section also doesn't indicate that zero is an invalid value for length .

Now, the Linux man page for recv() indicates that:

The value 0 may also be returned if the requested number of bytes to receive from a stream socket was 0.

And here comes the first BSD* man page I found, the OpenBSD man page for recv() which seems similar if not identical to others BSD* distros; and which says:

These calls return the number of bytes received, or -1 if an error occurred.

Reading the man page, it seems that the BSD implementation prefer to return an error on connection closure ( ECONNRESET ?) rather than returning zero.

My question therefore is... What in the hell is happening here? What model should I follow to make sure my library works with all recv() implementations? Is it normal that none of these implementation seem to respect the POSIX model? Is returning zero even a thing for BSD recv() ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM