3

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.

I think timeout is double the set value.

how is it possible?

Vijay Kumbhani
  • 734
  • 1
  • 6
  • 26
  • Did you check to make sure `setsockopt()` is not returning an error? What code are you expecting the timeout to apply to? Please provide a [SSCCE](http://sscce.org) that shows the double-time in action. – Remy Lebeau Oct 10 '14 at 17:14

2 Answers2

2

SO_RCVTIMEO does not accept an int as the timeout. You're looking for something like this:

struct timeval tv = {
    .tv_sec = 120
};

setsockopt(connSock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
Tom
  • 7,269
  • 1
  • 42
  • 69
1

SO_RCVTIMEO and SO_SNDTIMEO do not work on all socket operations, you should use non-blocking mode and 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. 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.

References

Discussion on this problem (read comments to answer):

How to use select to achive the desired result:

Community
  • 1
  • 1
Matteo Nardi
  • 199
  • 2
  • 7