简体   繁体   English

Socket SO_RCVTIMEO 超时是 C++/VC++ 中设置值的两倍

[英]Socket SO_RCVTIMEO Timeout is double the set value in C++/VC++

Please, show below example请显示以下示例

int val = 120000;
setsockopt(connSock,SOL_SOCKET,SO_RCVTIMEO,(char*)&val,sizeof(int));

I set 120 seconds at receive timeout but it takes 240 seconds.我在接收超时时设置了 120 秒,但需要 240 秒。

I think timeout is double the set value.我认为超时是设定值的两倍。

how is it possible?这怎么可能?

SO_RCVTIMEO and SO_SNDTIMEO do not work on all socket operations, you should use non-blocking mode and select . SO_RCVTIMEO 和 SO_SNDTIMEO 不适用于所有套接字操作,您应该使用非阻塞模式并select

The behaviour may change on different operating system configurations.该行为可能会在不同的操作系统配置上发生变化。 On my system the connect timeouts after two times the value I set in SO_RCVTIMEO.在我的系统上, connect超时是我在 SO_RCVTIMEO 中设置的值的两倍。 A quick hack like setting SO_RCVTIMEO to x/2 before a connect and x after it works, but the proper solution is using select.一个快速的黑客,如设置SO_RCVTIMEO到x/2之前,连接和x它的工作原理之后,但在适当的解决方案是使用选择。

References参考

Discussion on this problem (read comments to answer):关于这个问题的讨论(阅读评论回答):

How to use select to achive the desired result:如何使用 select 来实现所需的结果:

SO_RCVTIMEO does not accept an int as the timeout. SO_RCVTIMEO不接受int作为超时。 You're looking for something like this:你正在寻找这样的东西:

struct timeval tv = {
    .tv_sec = 120
};

setsockopt(connSock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM