epoll问题

yaohacker0225 2019-01-12 09:44:02
服务器端epoll接收数据,如果客户机网线断掉,或者客户机关机,服务器epoll如何监测到?还是服务器端需要其他机制检测,比如心跳之类的?
...全文
263 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
冷风1023 2019-01-15
  • 打赏
  • 举报
回复
可以通过设置心跳机制来控制
用setsockopt来设置探测间隔、发包间隔、次数
yaohacker0225 2019-01-13
  • 打赏
  • 举报
回复
引用 1 楼 northwesternwind的回复:
应该会作为事件返回。需要注册相应的事件:
EPOLLIN: 触发该事件,表示对应的文件描述符上有可读数据。(包括对端SOCKET正常关闭);
EPOLLOUT: 触发该事件,表示对应的文件描述符上可以写数据;
EPOLLPRI: 表示对应的文件描述符有紧急的数据可读(这里应该表示有带外数据到来);
EPOLLERR: 表示对应的文件描述符发生错误;
EPOLLHUP: 表示对应的文件描述符被挂断;
EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。
EPOLLONESHOT: 只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,需要再次把
谢谢,网络中断或者客户端关机,这些事件触发不了
northwesternwind 2019-01-12
  • 打赏
  • 举报
回复
应该会作为事件返回。需要注册相应的事件:
EPOLLIN: 触发该事件,表示对应的文件描述符上有可读数据。(包括对端SOCKET正常关闭);
EPOLLOUT: 触发该事件,表示对应的文件描述符上可以写数据;
EPOLLPRI: 表示对应的文件描述符有紧急的数据可读(这里应该表示有带外数据到来);
EPOLLERR: 表示对应的文件描述符发生错误;
EPOLLHUP: 表示对应的文件描述符被挂断;
EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。
EPOLLONESHOT: 只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,需要再次把
northwesternwind 2019-01-12
  • 打赏
  • 举报
回复
引用 2 楼 focuslight 的回复:
有 超时机制吧

下面内容从stackoverflow抄来的,应该 有用:
Sounds like you're trying to write an event loop (if so have a look at libev btw). epoll will not help you there, you have to keep track of socket inactivity yourself (clock_gettime() or gettimeofday() for instance), then wake up several times a second and check everything you need.

Some pseudo code

while (1) {
n = epoll_wait(..., 5);
if (n > 0) {
/* process activity */
} else {
/* process inactivity */
}
}
This will wake you up 200 times a second if all sockets are inactive.

The inactivity check requires a list of the sockets to be examined along with timestamps of the last inactivity:

struct sockstamp_s {
/* socket descriptor */
int sockfd;
/* last active */
struct timeval tv;
};

/* check which socket has been inactive */
for (struct sockstamp_s *i = socklist; ...; i = next(i)) {
if (diff(s->tv, now()) > 500) {
/* socket s->sockfd was inactive for more than 500 ms */
...
}
}
where diff() gives you the difference of 2 struct timevals and now() gives you the current timestamp.
Isnis-fallen 2019-01-12
  • 打赏
  • 举报
回复
有 超时机制吧

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧