非阻塞TCP与EPOll连接 [英] non blocking tcp connect with 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屋!