WinSock recv()超时:setsockopt()-设置值+半秒?
我正在编写一个跨平台库,除其他外,它提供了一个套接字接口,在运行我的单元测试套件时,我注意到通过setsockopt()设置的超时有些奇怪: Windows 中,阻塞的 recv()
调用似乎始终比通过 SO_RCVTIMEO
选项指定的时间晚大约半秒(500 毫秒)返回。
我错过的文档中有对此有任何解释吗?搜索网络,我只能找到 对问题的其他参考 – 拥有 »Windows Sockets 的人可以吗 网络编程 « 作者:Bob Quinn 和 Dave Shute 帮我查找第 466 页吗?不幸的是,我现在只能运行我的测试 Windows Server 2008 R2,其他 Windows 版本上是否也存在同样的奇怪行为?
I am writing a cross-platform library which, among other things, provides a socket interface, and while running my unit-test suite, I noticed something strange with regard to timeouts set via setsockopt()
: On Windows, a blocking recv()
call seems to consistently return about half a second (500 ms) later than specified via the SO_RCVTIMEO
option.
Is there any explanation for this in the docs I missed? Searching the web, I was only able to find a single other reference to the problem – could somebody who owns »Windows Sockets
Network Programming« by Bob Quinn and Dave Shute look up page 466 for me? Unfortunately, I can only run my test Windows Server 2008 R2 right now, does the same strange behavior exist on other Windows versions as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 Jones 和 Ohlund 的 Microsoft Windows 网络编程:
我认为其中的关键信息是:
我希望这仍然有用:)
From Networking Programming for Microsoft Windows by Jones and Ohlund:
I'd think the crucial information in this is:
I hope this is still useful :)
我也有同样的问题。将使用
patchedTimeout = max ( unpatchedTimepit - 500, 1 )
使用 unpatchedTimepit == 850 对此进行测试
I am having the same problem. Going to use
patchedTimeout = max ( unpatchedTimepit - 500, 1 )
Tested this with the unpatchedTimepit == 850
你的问题不在于rcv函数超时!
如果您的应用程序有一个 while 循环来检查和接收,只需放置一个 if 语句来检查接收缓冲区最后一个索引“\0”字符,以检查接收字符串是否结束。
通常,如果 rcv 函数仍在接收,则返回值是接收到的数据的大小。 size 可以用作缓冲区数组的最后一个索引。
your problem is not in rcv function timeout!
if your application have a while loop to check and receive just put an if statement to check the receive buffer last index for '\0' char to check is the receiving string is ended or not.
typically if rcv function is still receiving return value is the size of received data. size can be used as last index of buffer array.