Brian
Brian

Reputation: 3585

Tuning socket connect call timeout

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

Answers (3)

user207421
user207421

Reputation: 311052

No, this is not possible. The default connect timeout can be decreased, but not increased.

Upvotes: 0

kuchi
kuchi

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

fhe
fhe

Reputation: 6187

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.

Upvotes: 2

Related Questions