关于非阻塞connect和select设置
来源:互联网 发布:混合矩阵 报价 编辑:程序博客网 时间:2024/06/05 01:55
发现socket api中对connect进行超时判断,期间可能疏忽的一问题~~~
如果设置connect为非阻塞函数后, 进行select时只关注writefds,忽略exceptfds,可能出现一个问题:本来不想由于connect阻塞等太久,结果用select后反而傻等了。
看msdn对select中的exceptfds参数的说明:
int select(
__in int nfds,
__in_out fd_set* readfds,
__in_out fd_set* writefds,
__in_out fd_set* exceptfds,
__in const struct timeval* timeout
);
The parameter exceptfds identifies the sockets that are to be checked for the presence of OOB data or any exceptional error conditions.
Note Out-of-band data will only be reported in this way if the option SO_OOBINLINE is FALSE. If a socket is processing a connect call (nonblocking), failure of the connect attempt is indicated in exceptfds (application must then call getsockopt SO_ERROR to determine the error value to describe why the failure occurred). This document does not define which other errors will be included.
标志红色的大概意思是说:在进行非阻塞connect时,失败的事件是放在exceptfds中的……
那么, 如果在connect后开始select,只关注writefds,设置的超时是10秒, 在connect发出[SYN]后:
假定目标IP的主机不存在或者是目标端口给防火墙过滤了,那么你等再久也不会有任何回复, 这时候如果是阻塞connect可能要15秒才返回, 那么你10秒就返回了, 这种情况就赚了5秒
然而假定connect的目标IP主机是存在的也没防火墙,只是端口是没打开的,在connect发出[SYN]后的1秒系统已经收到目标主机回复的[RST, ACK],也就是说系统此时已经知道这个端口是连接不上的了, 但是应用程序只关注writefds,后面的9秒钟select就会傻傻的等待下去……原本以为用select来减少不必要的等待时间, 如果不设置参数exceptfds,这时候反而浪费时间。
貌似网上讨论connect设置超时的话题已经是很老了, 百度出来的也N多页,我有限的翻了一些看看, 貌似都没人理会 参数exceptfds,……
- 关于非阻塞connect和select设置
- 非阻塞recvfrom和connect的设置
- 非阻塞connect select问题
- connect超时设置.阻塞和非阻塞 ioctlsocket函数
- 非阻塞的connect函数和select函数
- 非阻塞的connect函数和select函数
- 关于connect前设置非阻塞的问题
- 非阻塞connect()和accept()
- 非阻塞connect()和accept()
- 非阻塞connect()和accept()
- 非阻塞connect()和accept()
- 阻塞,非阻塞connect()和accept()
- connect()函数阻塞和非阻塞问题解决
- select与阻塞和非阻塞
- 关于TCP非阻塞方式connect连接
- [Linux]关于非阻塞socket调用connect
- 非阻塞socket调用connect, epoll和select检查连接情况示例
- 非阻塞socket调用connect, epoll和select检查连接情况示例
- 如何遍历与某个文档关联的所有视图?
- bat截取变量
- Java exception
- 转帖:静态链接库与动态链接库
- 记录点击按钮次数
- 关于非阻塞connect和select设置
- ECC6.0与R/3的区别
- 如何删除某个控件的事件
- A股1991年到2010年所有日数据 有什么用??
- HTC Hero内置播放器的视频编解码测试
- 2010年IT行业从业者掌握哪六种技能将大受欢迎?
- 如何遍历当前打开的所有文档列表?
- 6410平台LCD显示的一个奇怪问题
- 面向对象C语言(Objective-C)编程简介