非阻塞 tcp 与 epoll 连接 [英] non blocking tcp connect with epoll

查看:33
本文介绍了非阻塞 tcp 与 epoll 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 linux 应用程序正在执行非阻塞 TCP 连接系统调用,然后使用 epoll_wait 检测三向握手完成.有时 epoll_wait 会同时返回 POLLOUT &POLLERR 为同一个套接字描述符设置的事件.

My linux application is performing non-blocking TCP connect syscall and then use epoll_wait to detect three way handshake completion. Sometimes epoll_wait returns with both POLLOUT & POLLERR events set for the same socket descriptor.

我想了解 TCP 级别发生了什么.我无法按需重现它.我的猜测是,在我的事件循环中对 epoll_wait 的两次调用之间,我们有一个 SYN+ACK/ACK/FIN 序列,但我再次无法重现它.

I would like to understand what's going on at TCP level. I'm not able to reproduce it on demand. My guess is that between two calls to epoll_wait inside my event loop we had a SYN+ACK/ACK/FIN sequence but again I'm not able to reproduce it.

推荐答案

如果连接失败,很可能会发生这种情况 - 例如连接超时"(对于执行非阻塞连接的套接字,POLLOUT 在成功和不成功的连接操作都完成时设置.

It is likely for this to happen if the connect has failed - for example with "connection timed out" (for sockets doing a non-blocking connect, POLLOUT becomes set when the connect operation has finished for both successful and unsuccessful outcomes).

POLLOUT 设置为套接字时,使用 getsockopt(sock, SOL_SOCKET, SO_ERROR, ...) 检查连接是否成功(SO_ERROR 套接字选项在这种情况下为 0,否则表示连接失败的原因).

When POLLOUT becomes set for the socket, use getsockopt(sock, SOL_SOCKET, SO_ERROR, ...) to check if the connect succeeded or not (the SO_ERROR socket option is 0 in this case, and otherwise indicates why the connect failed).

这篇关于非阻塞 tcp 与 epoll 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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