Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Dec : Re: how to check connection?

www.cryer.info
Managed Newsgroup Archive

Re: how to check connection?

Subject:Re: how to check connection?
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Fri, 28 Dec 2007 16:19:01

"soonic" <xxsoonic@op.pl> wrote in message
news:477574bc@newsgroups.borland.com...

>    while not Terminated do

Check the client's Connected() method as well:

    while (not Terminated) and FConn.Connected do

The thread may not have disconnected but the server on the other end may
have.

>    if IdTCPServer.Active = False then exit;
>    IdTCPServer.Active:=False;

Don't exit, otherwise you miss a chance to clean up the reading thread:

    if IdTCPServer.Active then IdTCPServer.Active := False;

>    if ReadingThread <> nil then ReadingThread.Terminate;
>    if IdTCPClient.Connected then IdTCPClient.Disconnect;
>  end;
>  if ReadingThread <> nil then
>  begin
>    ReadingThread.WaitFor;
>    FreeAndNil(ReadingThread);
>  end;

Wrap the Disconnect() in a try..except or try..finally so the thread is
still terminated and freed:

    if ReadingThread <> nil then
    begin
        ReadingThread.Terminate;
        try
            IdTCPClient.Disconnect;
        finally
            ReadingThread.WaitFor;
            FreeAndNil(ReadingThread);
        end;
    end;

> I use option with Synchronize(DisconnectServer). It fires automatically
> when I unplug the network cable

I would not rely on that behavior.  Many OS versions don't handle that
correctly.


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive