TIdTCPClient.Connect(Timeout not working)

TIdTCPClient.Connect(Timeout not working)

Post by Jamie Dal » Tue, 04 Dec 2007 08:51:45


Hi

Indy v9...

TIdTCPClient.Connect's timeout doesn't seem to work!

I've narrowed it down to one annoying little area:

procedure TIdConnectThread.Execute;
begin
try
// Id_WSAEBADF (9) on Linux, Id_WSAENOTSOCK (10038) on Windows
GStack.CheckForSocketError(FBinding.Connect, [Id_WSAEBADF,
Id_WSAENOTSOCK]);
except on
E: Exception do begin
FExceptionMessage := E.Message;
if E is EIdSocketError then begin
FLastSocketError := EIdSocketError(E).LastError;
end;
end;
end;
// Necessary as caller checks this
Terminate;
end;

Why 'Terminate' here?

Then in IdIOHandlerSocket:

if Terminated then begin
ATimeout := 0;
Break;
end;

It seems that for some reason, the connect thread is terminated prematurely
which nulls any connection timeout.

Why is the timeout there included if it is cancelled? - Or am I
misunderstanding it's purpose? (if I am, please explain!)

Thx

JD
 
 
 

TIdTCPClient.Connect(Timeout not working)

Post by Jamie Dal » Tue, 04 Dec 2007 08:55:23

I just thought, I should mention that I was testing the timeout by trying to
connect to a non-existent server address...

Didn't work though :(

JD

 
 
 

TIdTCPClient.Connect(Timeout not working)

Post by Team » Wed, 05 Dec 2007 05:48:50


Works fine for me.


For exactly the reason the comment says. TIdIOHandlerSocket.ConnectClient()
checks the thread's Terminated property to know whether the OS aborted the
call to connect() earlier than the timeout elapsed, or whether the timeout
was reached.


That is perfectly valid and common. The OS can exit from connect() as soon
as it knows a connection is either successful or failed. It does not have
to wait for the full timeout to elasped first. In fact, there is no timeout
feature available for blocking sockets to begin with. That is why Indy has
to use a manual loop instead. The timeout is the maximum amount of time
that Indy will wait. If the OS aborts earlier than the timeout, so be it.
Otherwise, Indy will close the socket manually if it was not connected (the
thread is still running when the loop is finished).


It is not cancelled.


Apparently so.


Gambit
 
 
 

TIdTCPClient.Connect(Timeout not working)

Post by Team » Wed, 05 Dec 2007 05:49:44


That is perfectly fine. Again, as soon as the OS knows that the connection
cannot succeed, it will abort the connect() immediately, before the full
timeout has elapsed.


It works fine for me.


Gambit