从SO_RCVTIMEO
手册页的部分socket
:
...if no data has been transferred and the time‐out has been reached,
then -1 is returned with errno set to EAGAIN or EWOULDBLOCK, or
EINPROGRESS (for connect(2)) just as if the socket was specified to
be non‐blocking.
从手册页recv
:
These calls return the number of bytes received, or -1 if an error
occurred. In the event of an error, errno is set to indicate the
error.
When a stream socket peer has performed an orderly shutdown, the
return value will be 0 (the traditional "end-of-file" return).
Datagram sockets in various domains (e.g., the UNIX and Internet
domains) permit zero-length datagrams. When such a datagram is
received, the return value is 0.
The value 0 may also be returned if the requested number of bytes to
receive from a stream socket was 0.
调用recv
将0
在断开连接时返回,或者如果接收到零长度数据报,或者如果请求的字节数是0
.
调用recv
将返回-1
任何错误,包括超时。您需要检查errno
以区分超时与其他一些错误。