Is there any way in a Win32 environment to "tune" the timeout on a socket connect()
call? Specifically, I would like to increase the timeout length. The sockets in use are non-blocking. Thanks!
Asked
Active
Viewed 2,850 times
3 Answers
2
Yes, this is possible.
If you're in non-blocking mode after connect()
, you normally use select()
to wait till I/O is ready. This function has a parameter for specifying the timeout value and will return 0 in case of a timeout.

fhe
- 6,099
- 1
- 41
- 44
-
No, this is not possible. The default connect timeout can be decreased but not increased. – user207421 Oct 09 '13 at 00:23
0
No, this is not possible. The default connect timeout can be decreased, but not increased.

user207421
- 305,947
- 44
- 307
- 483
0
You can try to use SO_RCVTIMEO and SO_SNDTIMEO socket options to set timeouts for any socket operations. Example:
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
if (setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout)) < 0)
error("setsockopt failed\n");
if (setsockopt (sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
sizeof(timeout)) < 0)
error("setsockopt failed\n");
You can also try alarm(). Sample:
signal( SIGALRM, connect_alarm ); /* connect_alarm is you signal handler */
alarm( secs ); /* secs is your timeout in seconds */
if ( connect( fd, addr, addrlen ) < 0 )
{
if ( errno == EINTR ) /* timeout, do something below */
...
}
alarm( 0 ); /* cancel the alarm */

kuchi
- 840
- 11
- 19
-
Receive and send timeouts have nothing to do with the connection timeout. – user207421 Oct 09 '13 at 00:24