- 论坛徽章:
- 0
|
原帖由 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的超时时间. |
|