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

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

问题描述

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

解决方案

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

使用它非常简单,只需将该标志与您要赋予epoll_ctl的任何其他标志或"即可.因此,例如,代替EPOLLIN编写EPOLLIN|EPOLLRDHUP.

epoll_wait之后,先执行if(my_event.events & EPOLLRDHUP),然后执行另一步(如果另一端关闭了连接,则可能要执行的操作)(您可能希望关闭套接字).

请注意,从套接字 读取时获得零字节读取"结果,这也意味着另一端已关闭连接,因此您也应该始终检查该连接,以避免令人讨厌的意外(FIN可能在之后EPOLLIN醒来,​​但之前在您呼叫read之前到达,如果您处于ET模式,则不会另一个通知).

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

解决方案

You use EPOLLRDHUP to detect peer shutdown, not EPOLLHUP (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 of EPOLLIN write 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).

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天全站免登陆