我如何使用 EPOLLHUP [英] How do I use EPOLLHUP

查看:36
本文介绍了我如何使用 EPOLLHUP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

您使用 EPOLLRDHUP 来检测对等端关闭,而不是 EPOLLHUP(它表示套接字意外关闭,即通常是内部错误).

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

使用它非常简单,只需将标志与您提供给 epoll_ctl 的任何其他标志或"即可.因此,例如用 EPOLLIN|EPOLLRDHUP 代替 EPOLLIN.

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 EPOLLIN write EPOLLIN|EPOLLRDHUP.

epoll_wait 之后,做一个 if(my_event.events & EPOLLRDHUP) 然后是如果对方关闭连接你想做的任何事情(你可能会想关闭socket).

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

请注意,从套接字读取时获得读取零字节"结果意味着另一端已关闭连接,因此您也应始终检查这一点,以避免出现令人讨厌的意外(FIN 可能在你从 EPOLLIN 中醒来后到达,但在你调用 read 之前代码>,如果您处于 ET 模式,您将不会收到其他通知).

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 FIN might arrive after you have woken up from EPOLLIN but before you call read, if you are in ET mode, you'll not get another notification).

这篇关于我如何使用 EPOLLHUP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆