Could you guys provide me a good sample code using EPOLLHUP for dead peer handling? I know that it is a signal to detect a user disconnection but not sure how I can use this in code..Thanks in advance..
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You use
EPOLLRDHUPto detect peer shutdown, notEPOLLHUP(which signals an unexpected close of the socket, i.e. usually an internal error).Using it is really simple, just “or” the flag with any other flags that you are giving to
epoll_ctl. So, for example instead ofEPOLLINwriteEPOLLIN|EPOLLRDHUP.After
epoll_wait, do anif(my_event.events & EPOLLRDHUP)followed by whatever you want to do if the other side closed the connection (you’ll probably want to close the socket).Note that getting a “zero bytes read” result when reading from a socket also means that the other end has shut down the connection, so you should always check for that too, to avoid nasty surprises (the
FINmight arrive after you have woken up fromEPOLLINbut before you callread, if you are in ET mode, you’ll not get another notification).