Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
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()
.
–
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
-
Please be sure to
answer the question
. Provide details and share your research!
But
avoid
…
-
Asking for help, clarification, or responding to other answers.
-
Making statements based on opinion; back them up with references or personal experience.
To learn more, see our
tips on writing great answers
.