关于非阻塞connect和select设置
2010-02-02 15:24
295 查看
发现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时只关注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前设置非阻塞的问题
- 关于用js和attr("selected",true);给select设置选中的值失效问题
- 非阻塞模式的设置、设置socket为非阻塞模式 解决connect阻塞问题
- 阻塞TCP套接字connect超时设置
- 关于<c:forEach>中select和oprtion设置默认选项
- 阻塞与非阻塞、select关于深入网络编程
- 关于有时select下拉表单selected设置无效的解决方案
- 关于jquery设置select中option值
- Django forms 关于select和checkbox设置初始选中值
- 关于用js和attr("selected",true);给select设置选中的值失效问题
- 非阻塞Connect对于select时应注意问题
- 非阻塞connect select问题
- 关于ViewPager设置属性页setCurrentItem会阻塞主线程ANR总结
- Winsock例子代码:初始化、阻塞/非阻塞、超时设置、select等
- 非阻塞socket调用connect, epoll和select检查连接情况示例
- 关于chosen-select设置选中值和清空选中值的问题
- 非阻塞socket调用connect, epoll和select检查连接情况示例
- 非阻塞Connect对于select时应注意问题
- 非阻塞socket下客户端connect + select简单demo
- 关于用js和attr("selected",true);给select设置选中的值失效问题