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

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

问题描述

我的Linux应用程序正在执行非阻塞TCP连接syscall,然后使用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.

推荐答案

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

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