Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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 */

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top