Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timeout for Socket "connect" function

Status
Not open for further replies.

Bobby2001

Programmer
Dec 12, 2002
4
CA
Please take a look at the segment of code below:

SOCKET sock = socket( AF_INET, SOCK_STREAM, 0 );
if ( sock == INVALID_SOCKET )
{
SetErrorCode(WSAGetLastError());
return FALSE;
}

int nRet = connect( sock, (PSOCKADDR)&remoteAddr, sizeof(remoteAddr));
if (nRet == SOCKET_ERROR )
{
SetErrorCode(WSAGetLastError());
return FALSE;
}

The problem is that when I specify non-existed IP address (but within valid range of IP addresses for our network) "connect" function doesn't return for 40 seconds or so. Can anyone advice how to configure timeout for "connect" function? Or how can I make this waiting time shorter? Thanks
 
Hi,

Can't you use setsockopt(...) for this?

Code:
int timeout = TIMEOUT_VALUE_IN_MS ;
int err ;

SOCKET sock = socket( AF_INET, SOCK_STREAM, 0 ) ;

err = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,(char *)&timeout,sizeof(timeout));
if (err != NO_ERROR) 
{
    // Oops
}

HTH

--
William
Software Engineer
ICQ No. 56047340
 
Hi Bobby and Wills, I had a similar problem some time back and simply gave up on it. I tried just about everything but to no avail. According to MSDN, the SO_RCVTIMEO and SO_SNDTIMEO options are not available with SetSocketOption(). If anyone ever finds a way around this, I would be very interested to see it!
If there is an answer then it's probably quite a simple one - but sometimes, when you're tearing your hair out, you can't see the wood for the trees!!
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
gednick,

This is true for the CAsyncSocket class but the above code uses the setsockopt API function as supported in MS Sockets 2.

--

William
Software Engineer
ICQ No. 56047340
 
Of course - how silly of me! Works exactly the same as the unix sockets.

You would think though that Microsoft, when wrapping the sockets into MFC, would at least implement the time out option in the wrapper!

:-(
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Hi guys,

Thank you for your responses. It seems very strange to me that one can use setsockopt function to set timeouts. Look at the link below, the Microsoft clearly list options SO_RCVTIMEO and SO_SNDTIMEO as not supported by setsockopt function. Are they wrong?

msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/setsockopt_2.asp

I've solved my original problem by using non-blocking mode in sockets.
 
Hi Bobby, as William so kindly reminded me that your original code uses neither of the MFC socket wrappers. In fact, you are using typical plain old fashion unix style sockets which are supported by winsock.
Anyway, the [unix style] setsockopt() function should actually work to set a time out in this case. It is the MFC wrapper CAsyncSocket::SetSockOpt() that does not support SO_RCVTIMEOUT, etc. (for some incredibly strange reason!).

Non-blocking sockets are not that bad. I tend to use these all the time now (in a separate thread). I feel that I have more control over the sockets that way!
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top