Re: Client disconnection detection

Giganews Newsgroups
Subject: Re: Client disconnection detection
Posted by:  Remy Lebeau (TeamB) (
Date: Tue, 24 Aug 2004

"Simon A." <saberu…> wrote in message

> When the server is not writing or reading from it
> clients, it doesn't detect the clients deconnection.

Of course, because you are not accessing the socket at all during that time.
You have to access the socket in order to check if it is still alive and

> And I've found that it take 2 writings from the server
> to detect that there is a error with the client's connection.

If the socket was closed gracefully by the client, the server will detect it
immediately.  It is only abnormal disconnects that cannot be detected right
away, by design of the underlying socket stack, not Indy.  The stack itself
doesn't know right away that the socket has actually been lost, thus it
continues accepting data from the application until the stack's internal
timeouts and such have a chance to elapse and eventually start reporting the
status of invalidated sockets.

> Is there a way to detect if a client is still there and that the
> ondisconnect event is called whenever a client disconnect
> from the server even if the server is not reading or writing
> anythig to it client?

The OnDisconnect event is triggered whenever the TIdPeerThread is
terminated.  Unless you disconnect the socket yourself, the thread will not
terminate until the socket actually begins to report errors, which will not
happen right away unless the client disconnects from the server in a
graceful negotiated manner.  Abnormal disconnects simply cannot be detected
in a timely fashion because the underlying socket stack simply does not know
right away that the sockets have been lost to begin with.

If that is not good enough for your needs, then you will just have to
implement some kind of keepalive packets in your communications so that
clients can actively tell the server periodically that they are still
present and alive.  If the server does not receive a keepalive in a timely
fashion, it can then choose to close the socket at that point.



In response to

Client disconnection detection posted by Simon A. on Tue, 24 Aug 2004