From the manual of epoll_ctl:
EPOLLRDHUP (since Linux 2.6.17)
Stream socket peer closed connection, or shut down writing half of connection. (This flag is especially useful for writing simple code to detect peer shutdown when using Edge Triggered monitoring.)
From the manual of recv:
If no messages are available to be received and the peer has performed an orderly shutdown, recv() shall return 0.
It seems to me then that both of the above cover the same scenarios, and that as long as I catch EPOLLRDHUP events first, I should never receive a read() or recv() of length 0 (and thus don't need to bother checking for such). But is this guaranteed to be true?
If you get an event with EPOLLRDHUP=1
then just close the connection right away without reading. If you get an event with EPOLLRDHUP=0
and EPOLLIN=1
then go ahead and read, but you should be prepared to handle the possibility of recv()
still returning 0, just in case. Perhaps a FIN
arrives after you got EPOLLIN=1
but before you actually call recv()
.
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