免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: jeanlove
打印 上一主题 下一主题

[C] 请教,我的select函数的timeval不起作用? [复制链接]

论坛徽章:
0
1 [报告]
发表于 2009-05-12 23:28 |显示全部楼层
提两个建议:
1. 格式化你的代码.
2. 你的代码还是不最简, 要用最简的代码来验证你的疑问, 避免无关代码的影响.

论坛徽章:
0
2 [报告]
发表于 2009-05-13 10:59 |显示全部楼层
原帖由 jeanlove 于 2009-5-13 10:15 发表


问题是没有任何服务器在运行,我只是写了一个client端,看看select是否有等待的作用。

但是问题是,运行结果没有等待,立刻结束了。

为什么呢

引用UNP:

Berkeley-derived implementations (and POSIX) have the following two rules regarding select and nonblocking connects:

When the connection completes successfully, the descriptor becomes writable (p. 531 of TCPv2).

When the connection establishment encounters an error, the descriptor becomes both readable and writable (p. 530 of TCPv2).


These two rules regarding select fall out from our rules in Section 6.3 about the conditions that make a descriptor ready. A TCP socket is writable if there is available space in the send buffer (which will always be the case for a connecting socket since we have not yet written anything to the socket) and the socket is connected (which occurs only when the three-way handshake completes). A pending error causes a socket to be both readable and writable.


翻译解释一下, 一个socket的发送缓冲如果未满, 则可写(对于一个连接中的socket, 总是可写的, 因为我们还未写入任何数据). 如果发生连接错误, 也会让socket可读和可写. 服务器没有启动, 所以肯定会发生连接错误, 这时, 连接已可读可写, select没有必要等待超时.

楼主可以看一下"UNP"这本书的16.4 Nonblocking connect: Daytime Client.  如果你想得到"尝试连接服务器, 最多尝试10秒"这个结果, 显然你不能仅仅通过select, 你应该维护你自己的计时器, 并在连接失败后手工重试(重新select), 并调整select的超时时间.

论坛徽章:
0
3 [报告]
发表于 2009-05-13 11:05 |显示全部楼层
如果根本没有服务器端运行,这个select就根本不等待,而是直接返回一个错误状态。

正是这样的.
只有连上了,在等待IO的时候,select才是起作用的?

不能随便地得出这种推论.

对于你的问题的解决方法: 检测到连接失败, 重新connect.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP