gpt4 book ai didi

windows - WinSock recv() 超时 : setsockopt()-set value + half a second?

转载 作者:可可西里 更新时间:2023-11-01 10:28:39 25 4
gpt4 key购买 nike

我正在编写一个跨平台库,它提供了一个套接字接口(interface),在运行我的单元测试套件时,我注意到通过 setsockopt() 设置的超时有一些奇怪的地方>:在 Windows 上,阻塞的 recv() 调用似乎始终比通过 SO_RCVTIMEO 选项指定的时间晚返回大约半秒(500 毫秒)。

我遗漏的文档中是否对此有任何解释?在网上搜索,我只能找到 a single other reference问题 – 拥有 »Windows Sockets 的人可以吗Bob Quinn 和 Dave Shute 的网络编程« 帮我查找第 466 页?不幸的是,我现在只能运行我的测试 Windows Server 2008 R2,其他 Windows 版本是否也存在同样的奇怪行为?

最佳答案

来自 Networking Programming for Microsoft Windows琼斯和奥伦德:

SO_RCVTIMEO optval


  • Type: int
  • Get/Set: Both
  • Winsock Version: 1+
  • Description : Gets or sets the timeout value (in milliseconds) associated with receiving data on the socket

The SO_RCVTIMEO option sets the receive timeout value on a blocking socket. The timeout value is an integer in milliseconds that indicates how long a Winsock receive function should block when attempting to receive data. If you need to use the SO_RCVTIMEO option and you use the WSASocket function to create the socket, you must specify WSA_FLAG_OVERLAPPED as part of WSASocket's dwFlags parameter. Subsequent calls to any Winsock receive function (such as recv, recvfrom, WSARecv, or WSARecvFrom) block only for the amount of time specified. If no data arrives within that time, the call fails with the error 10060 (WSAETIMEDOUT). If the receiver operation does time out the socket is in an indeterminate state and should not be used.

For performance reasons, this option was disabled in Windows CE 2.1. If you attempt to set this option, it is silently ignored and no failure returns. Previous versions of Windows CE do implement this option.

我认为这里的关键信息是:

If you need to use the SO_RCVTIMEO option and you use the WSASocket function to create the socket, you must specify WSA_FLAG_OVERLAPPED as part of WSASocket's dwFlags parameter

我希望这仍然有用:)

关于windows - WinSock recv() 超时 : setsockopt()-set value + half a second?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6234086/

25 4 0
文章推荐: PHP 显示 CMD 命令提示符窗口 (Windows)
文章推荐: java.lang.NoClassDefFoundError : org/apache/axis2/AxisFault When axis2-kernel-1. 5.4.jar在类路径中
文章推荐: c# - 多线程代理检查器
文章推荐: css - 如何隐藏HTML