Discussion:
Winsock- connect time out issues
(too old to reply)
quest
2005-09-27 06:39:49 UTC
Permalink
I could use select() function to impose time out value on connect()
function. I did a simple test. Consider the following scenario:

1. Impose 20 seconds time out value on connect() function (using select()).
2. Run client connect application that attempts to connect a specific
server.
3. After 5 seconds, i started the listening server.

The problem here is the client will not be able to connect to server. It
will just time out even though the server is now running. Any idea why this
is happening ? I though connect() function is supposed to try to connect to
server within the imposed time out value. Thanks.
Igor Tandetnik
2005-09-27 11:37:19 UTC
Permalink
Post by quest
I could use select() function to impose time out value on connect()
function.
select() does not impose any time-out on the operation in progress.
select specifies how long your application is willing to wait for
something to happen to the socket, not how long the socket should wait
for something to happen on the remote end. The connect timeout is not
configurable - at least not programmatically, I seem to vaguely remember
there might be a regisry setting that controls it. I believe the default
timeout is much smaller than 20 sec

I suspect one of the following happens:
1. You did not switch the socket to the non-blocking mode. You call
connect(), it blocks for a couple of seconds then returns with
WSAETIMEOUT error. You don't check for error (assuming that the socket
is non-blocking and the connection attempt has not yet taken place) and
call select(). Of course since nothing happens on the socket, select()
has nothing to report and returns only when its timeout expires.

2. You did switch the socket to the non-blocking mode and called
connect() followed by select(). However, you specified the socket in
writefds set but not in exceptfds set. connect() fails within a couple
of seconds, but your select() call does not check for that. Since the
socket never becomes writable, select() only returns when its timeout
expires.

3. You did everything correctly, and select() returned within a couple
of seconds notifying you of the error. For some reason, you believe that
select() is supposed to retry a failed operation on your behalf, which
of course it doesn't. So when it returns and the socket is still not
connected, you just assume that it waited for the full 20 sec without
actually measuring it.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Michael K. O'Neill
2005-09-27 15:17:23 UTC
Permalink
Post by quest
I could use select() function to impose time out value on connect()
1. Impose 20 seconds time out value on connect() function (using select()).
2. Run client connect application that attempts to connect a specific
server.
At this point, your client has sent out its SYN packet, in its attempt to
open a connection. This is the first step of the "three-way handshake", by
which TCP/IP connections are established.
Post by quest
3. After 5 seconds, i started the listening server.
The server has completely missed the SYN packet, whaich has already been
sent and ignored by the time the server is started, and there's no way a
connection can ever be established.
Post by quest
The problem here is the client will not be able to connect to server. It
will just time out even though the server is now running. Any idea why this
is happening ? I though connect() function is supposed to try to connect to
server within the imposed time out value. Thanks.
connect() does not make repeated attempts to connect. It makes one attempt,
and that attempt failed because the server was not there to see the SYN
packet.
quest
2005-09-29 04:00:07 UTC
Permalink
Yes.. Thanks for the comments. Thanks to Igor's comment too.
Post by quest
Post by quest
I could use select() function to impose time out value on connect()
1. Impose 20 seconds time out value on connect() function (using
select()).
Post by quest
2. Run client connect application that attempts to connect a specific
server.
At this point, your client has sent out its SYN packet, in its attempt to
open a connection. This is the first step of the "three-way handshake", by
which TCP/IP connections are established.
Post by quest
3. After 5 seconds, i started the listening server.
The server has completely missed the SYN packet, whaich has already been
sent and ignored by the time the server is started, and there's no way a
connection can ever be established.
Post by quest
The problem here is the client will not be able to connect to server. It
will just time out even though the server is now running. Any idea why
this
Post by quest
is happening ? I though connect() function is supposed to try to connect
to
Post by quest
server within the imposed time out value. Thanks.
connect() does not make repeated attempts to connect. It makes one attempt,
and that attempt failed because the server was not there to see the SYN
packet.
Continue reading on narkive:
Loading...