一开始不理解为什么设置了 timeout 的 recv 会返回 EAGAIN 错误,在我的知识体系中 EAGAIN 一般出现在非阻塞的 socket 中,后来 man 了一下 SO_RCVTIMEO,发现确实如此,给后面的同学提个醒:
Specify the receiving or sending timeouts until reporting an error. The argument is a struct timeval. If an input or output function blocks for this period of time, and data has been sent or received, the return value of that function will be the amount of data transferred; if no data has been transferred and the timeout 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 nonblocking.
总之一句话,SO_RCVTIMEO 会导致 recv 返回 EAGAIN
展开