Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Aug : Re: How to exit gracefully on listener thread when server crashes.

www.cryer.info
Managed Newsgroup Archive

Re: How to exit gracefully on listener thread when server crashes.

Subject:Re: How to exit gracefully on listener thread when server crashes.
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Fri, 18 Aug 2006 11:00:07

"Gerhard Venter" <IeatSpam4breakfast@spam.com> wrote in message
news:44e5f887$1@newsgroups.borland.com...

> I have a listener thread that has a TIdTCPClient connected to a server.
> The server runs on Linux and is not a Indy server. I am experiencing
> problems that my client app hangs when the server crashes for what
> ever reason. Although I have a readtimeout of 5000ms and check
> for disconnect it still hangs.

A server crash will not trigger a disconnect for a long time.  The read
timeout should be working fine, though.

> I would like to know how to exit gracefully in this situation.

If possible, implement a keep alive in your protocol.  Have the client send
a keep alive packet to the server periodically.  If the server does not
respond in a timely manner, then the client disconnect from the server.
This also allows the server to disconnect dead clients that do not send data
for awhile if they crashed.

> Here is the code I have on the Run method of  listener's thread (The
> listener thread is a TIdThread).

Get rid of the loop altogether, and get rid of the checks for a disconnect.
Run() is already looped by TIdThread, and ReadLn() already handles
disconnect detection.  An uncaught socket exception during reading/writing
will escape back into TIdThread and allow it to terminate itself gracefully.

    procedure TcsMyListenerThread.Run;
    var
        lData: String;
    begin
        try
            lData := FMyListener.IOHandler.ReadLn(cCRLF2x, 5000);
            if lData <> EmptyStr then
                FProxyServer.PublishEvent(lData);
        except
            on E: EIdConnClosedGracefully do FProxyServer.Stop('Connection
to the server has been closed by the server.');
            on E: EIdClosedSocket do FProxyServer.Stop('Connection to the
server has been lost. Disconnecting.');
            on E: Exception do FProxyServer.Stop('Error: ' + E.Message + '.
Disconnecting.');
        end;
    end;


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive