Reputation: 3585
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.
Upvotes: 2
Views: 2971
Reputation: 311052
No, this is not possible. The default connect timeout can be decreased, but not increased.
Upvotes: 0
Reputation: 860
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 */
Upvotes: 0