When the remote socket is disconnected, the TElSocket state (or the Active property of socket-enabled components) indicates that the connection is still present. Why?

The TCP protocol was designed to be tolerant to temporary failures of the carrier. And if you don't transfer any data over the established logical connection, the TCP stack remains dormant and does nothing to detect the state of this connection.

If the remote system gracefully closes the socket (i.e., sends "goodbye" packets) and the carrier is active, then the TCP stack on your computer is notified about the socket being closed. If the remote system closes the connection forcefully (computer is powered off, etc.) or when the carrier fails on the way (say, the satellite has fallen down or a shark has bitten the trans-Atlantic cable), the local system knows nothing about the connection state.

But in both cases, the Windows Sockets module doesn't have a way to notify the application about state change other than returning an error on an attempt to do something with the socket. So, the best you can do is to send something over the socket. And if there's no connection available anymore, you will receive an error that will tell you that the connection has died.

In some cases, it is important to know the current state of the connection in order to notify the user or another software module.
In this case, the only option is to use so-called keep-alive packets in the application-level protocol. Your application needs to send them periodically. For FTP this can be a request for a current directory. SSH/SFTP also offers keep-alive packets (these are supported in SecureBlackbox). In HTTP you can send an OPTIONS request. Some other protocols have the NOOP command or something similar. It is important that your code initiates the sending of such requests so it is notified about the failure (and disconnection) as soon as possible in this case.

Remember to call the Close() method of the corresponding SecureBlackbox component in case of disconnection or when you are done using the component. This will let the component release resources and be ready for subsequent use.

Ready to get started?

Learn more about SecureBlackbox or download a free trial.

Download Now