Linux 我如何使用 EPOLLHUP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6437879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:39:28  来源:igfitidea点击:

How do I use EPOLLHUP

linuxnetworkingepoll

提问by user800799

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..

你们能给我提供一个使用 EPOLLHUP 进行死对等处理的好的示例代码吗?我知道这是检测用户断开连接的信号,但不确定如何在代码中使用它..提前致谢..

采纳答案by Damon

You use EPOLLRDHUPto detect peer shutdown, not EPOLLHUP(which signals an unexpected close of the socket, i.e. usually an internal error).

您用于EPOLLRDHUP检测对等关闭,而不是EPOLLHUP(这表示套接字意外关闭,即通常是内部错误)。

Using it is really simple, just "or" the flag with any other flags that you are giving to epoll_ctl. So, for example instead of EPOLLINwrite EPOLLIN|EPOLLRDHUP.

使用它非常简单,只需将标志与您提供的任何其他标志“或”即可epoll_ctl。因此,例如而不是EPOLLINwrite EPOLLIN|EPOLLRDHUP

After epoll_wait, do an if(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).

After epoll_waitif(my_event.events & EPOLLRDHUP)如果对方关闭了连接(你可能想要关闭套接字),然后执行任何你想做的事情。

Note that getting a "zero bytes read" result when reading from a socket alsomeans that the other end has shut down the connection, so you should always check for that too, to avoid nasty surprises (the FINmight arrive afteryou have woken up from EPOLLINbut beforeyou call read, if you are in ET mode, you'll not get another notification).

请注意,从套接字读取时获得“读取零字节”结果意味着另一端已关闭连接,因此您应该始终检查这一点,以避免令人讨厌的意外(FIN可能会您醒来后到达EPOLLIN您致电之前read,如果您处于 ET 模式,您将不会收到其他通知)。