views:

83

answers:

1

Hello, I've written a Delphi (2009) app with Indy (TCPServer/Client). And I have a problem at the level of TCPServer : it detects the deconnection(after a reboot of the PC) of the Client only when the client tries one more time to reconnect to the server. I've executed my app step by step, and when the client tries top reconnect (after the reboot), the server raises (a silent, i think) exception : Connection reset by peer. Why this exception is too late.

(I Think the thread lister is not released immediately)

What do you think ?

+2  A: 

This is a low-level detail of the TCP protocol. By default, it doesn't transmit keep-alive packets, because that wastes bandwidth, so it will only realise that the connection has died when something happens that prompts it to check the connection. (I'm not sure why a reconnection from the same client would trigger this though.)

You can make TCP send regular keep-alive packets to discover disconnections earlier. In windows, this can be activated by the server application, but is configured via the registry (see here for details).

You may also be able to implement a keep-alive mechanism at the protocol level by getting the client or server to send occasional null packets (a packet that does nothing in particular). This obviously requires that the protocol you are using define some kind of do-nothing or do-nothing-interesting message (NOOP, STATUS, PING ... stuff like that).

Marcelo Cantos
Before, (few days ago), the system worked perfectly.By using OnDisconnect event pof TCPServer, it indicates at the real time when the connection was close, but now, without any code modify, it doesn't work (tested on 3 local networks) !
djiga4me
In addition to being globally configurable in the registry, you can also set it for one connection at a time with [`setsockopt`](http://msdn.microsoft.com/en-us/library/ms740476.aspx). Indy provides a wrapper, `GWindowsStack.SetSocketOption`, in the *IdStackWindows* unit.
Rob Kennedy
Not worked about do-nothing messages from the server.I'll not use the registry.
djiga4me
When it was working previously, were you closing the connection cleanly? TCP does have a mechanism for cleanly closing a connection (which you should be using whenever possible). In that case, the disconnect would be detected immediately because there was an explicit disconnect request sent.
Scott W
Sometime the connectione is close normally, but when the connection is not normally closed, the Server detects the deconnection !!But how detect the deconnection when the client is instantly closed (reboot of the system for example ?)!
djiga4me
A system reboot is not an instant closure. The system will try to send a quit message to all applications before closing. The more likely scenario is a power outage or an ethernet cable accidentally pulled out.
Marcelo Cantos
No, it's not a noprmal system reboot. But when we click on the reboot button on the PC.
djiga4me
If you mean the "reset" button instead, then there is no way for a server to detect that in a timely manner. Pressing that button is immeidately killing the OS and all contents of memory, putting the computer back into a clean bootup state. The only way the server can detect that is via either TCP-level keep-alives, protocol-level keep-alives, or timeouts (the socket endpoint on the server end will eventually timeout and report errors, but not for potentially several hours). If you need to rely on timeouts alone, then you should implement your own manual timeout.
Remy Lebeau - TeamB
This is what i mean !So i'll try to implement a keep-alive system manually !
djiga4me
Finally, I found a solution.Sending info by the server and waiting for answer, if no answer received is a specified time, client is considered as disconnected !Thank you !
djiga4me