请教,vc 中设置 recv 超时 不准?

pccldr 2015-05-22 01:12:22
我写一小段代码在线程中 测试recv超时是否可用时发现系统最少有500ms 的超时。

int nNetTimeoutRX = 1;
recv_tick = GetTickCount();
setsockopt( sckLink, SOL_SOCKET, SO_RCVTIMEO, ( char * )&nNetTimeoutRX, sizeof( int ) );
iResult = recv(sckLink,(char *)&RecvHead,Head_Len,0);
PRPrint(M_PRINT_TYPE_NORMAL,L"time is %dms.",GetTickCount() - recv_tick);

结果测试如下:

[14:11:11 # 3] 已连接系统!
[14:11:12 # 4] time is 530ms.
[14:11:12 # 5] 连接已被终止(-1)


nNetTimeoutRX 改成 1000时

[14:12:29 # 3] 已连接系统!
[14:12:30 # 4] time is 1529ms.
[14:12:30 # 5] 连接已被终止(-1)

这是为什么啊? 还需要对哪里进行设置?谢谢各位
...全文
643 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿达AdaLovelace 2017-11-29
  • 打赏
  • 举报
回复
楼主的问题解决了嘛 ???????
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
Socket Option for NT 4.0 and Win95
SO_SNDTIMEO

SO_RCVTIMEO

Details on SO_SNDTIMEO and SO_RCVTIMEO
These two options set up timeouts for the send, sendto, recv, and recvfrom functions. You can obtain the same functionality by calling select with a timeout just before the I/O call, but these options offer a significant improvement in performance by avoiding a kernel transition and the other overhead of the select call. For any code whose performance is very critical, applications should use these timeout options rather than select.

You can set these options on any type of socket in any state. The default value for these options is zero, which refers to an infinite timeout. Any other setting is the timeout, in milliseconds. It is valid to set the timeout to any value, but values less than 500 milliseconds (half a second) are interpreted to be 500 milliseconds.

To set a send timeout, use

int timeout = TIMEOUT_VALUE;
int err;
SOCKET s;

s = socket( ... );
err = setsockopt(
s,
SOL_SOCKET,
SO_SNDTIMEO,
(char *)&timeout,
sizeof(timeout));
if (err != NO_ERROR) {
/* failed for some reason... */
}

The TIMEOUT_VALUE is the needed timeout in milliseconds. To set a receive timeout, substitute SO_RCVTIMEO for SO_SNDTIMEO in the preceding example.

After setting one of these options to a nonzero value, I/O through the Windows Sockets calls fails with the error WSAETIMEDOUT if the request cannot be satisfied within the specified timeout. If a request times out, an application has no guarantees as to how much data was actually sent or received in the I/O call.

The following socket option is used in conjunction with the MS Extension function AcceptEx.

SO_UPDATE_ACCEPT_CONTEXT


Colin_19861116 2015-10-13
  • 打赏
  • 举报
回复
我也是这样的,有没有人知道
赵4老师 2015-05-22
  • 打赏
  • 举报
回复
Windows计时精度≈15ms
翅膀又硬了 2015-05-22
  • 打赏
  • 举报
回复
windows系统本来就没法精确到毫秒级别
ok1234567 2015-05-22
  • 打赏
  • 举报
回复
这个,虽然配置上有毫秒精度,使用时一般都是用秒计的,而且系统保证超时只长不短:)
pccldr 2015-05-22
  • 打赏
  • 举报
回复
开发环境 WIN7 64BIT 专业版 + VS 2010

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧