Board index » delphi » How to check for Disconnect on TIdTcpClient

How to check for Disconnect on TIdTcpClient


2007-04-02 08:15:14 PM
delphi115
Hi,
I have a general question.
I want to receive an event both if a .Disconnect; call succeded and also
if the connection is closed due to another reason, i.e. an error occured.
Is it right to do it like that?
procedure TModuleProxy.TcpStatus(ASender:TObject; const AStatus :
TIdStatus; const AStatusText : String);
begin
If AStatus = hsDisconnected then begin
if readThread<>nil then begin
readThread.Terminate;
readThread.WaitFor;
FreeAndNil(readThread);
end;
If Assigned(conn.IOHandler) then
conn.IOHandler.InputBuffer.Clear;
buffer := '';
// Fire event to main application
If Assigned(FOnDisconnect) then
FOnDisconnect(self);
end;
end;
I'm asking because my apps seems to have a problem if a Disconnected
Status is Received shortly after I connected.
Thanks,
Michael
 
 

Re:How to check for Disconnect on TIdTcpClient

Michael Stieler schrieb:
Quote
Hi,

I have a general question.

I want to receive an event both if a .Disconnect; call succeded and also
if the connection is closed due to another reason, i.e. an error occured.

Is it right to do it like that?

procedure TModuleProxy.TcpStatus(ASender:TObject; const AStatus :
TIdStatus; const AStatusText : String);
begin
If AStatus = hsDisconnected then begin

if readThread<>nil then begin
readThread.Terminate;
readThread.WaitFor;
FreeAndNil(readThread);
end;
I found out you must not do this because the OnStatus event can be
triggered by the Read-Thread.
That is a dead-lock because the triggering thread waits for itself to
terminate.
So I have to use a kind of notifying here..
Michael
 

Re:How to check for Disconnect on TIdTcpClient

"Michael Stieler" <XXXX@XXXXX.COM>writes
Quote
I want to receive an event both if a .Disconnect; call succeded
and also if the connection is closed due to another reason, i.e.
an error occured.
Indy is not an event-driven library. There is no asynchronous
notification of the connection being closed. Yes, there is an
OnDisconnect event available, but it is only triggered when
Disconnect() is explicitally called. If there is a socket error, or
the other party has closed the connection, the only way to be notified
of that is to catch the exception that is raised the next time you
read from, or write to, the connection. This is by design.
Quote
I'm asking because my apps seems to have a problem if
a Disconnected Status is Received shortly after I connected.
Then your apps are likely not performing error handling correctly.
Gambit