Is there any event for connection disconnected because of timeout?

0 votes
asked Dec 12, 2012 by DS1 (160 points)
edited Dec 12, 2012

We are using FTP and SFTP protocols to connect to server and want to know when connection is closed by server because of timeout. Is there any event which is fired in this scenario?

As mentioned here ( the CheckConnectionState will not throw an exception when the connection is closed by the server. The reliable way which is suggested here is to send a command over the connection, is also not proper because every time we want to do directory listing, download file, upload file or any other operation we have to check if connection is opened and for each of this operation a command will be sent to the server.

Instead of sending a command before every operation, is there any event which is raised when the connection is lost?

1 Answer

0 votes
answered Dec 12, 2012 by Lukas Pokorny (117,510 points)
edited Dec 12, 2012

Actually, when the connection is closed gracefully, the CheckConnectionState should be able to detect it. However, if a connection is lost without the client machine being aware of it (and this actually happens on unstable connections), the only way to detect the failure is to send a packet to the server first. This means that the event would have to do that as well, which would kind-of defeat its purpose - the lost connection check would have to be performed periodically every few seconds and would not be able to detect the failure instantly.

Why not just wrap the FTP/SFTP code with catch (SftpException/FtpException), check the exception's Status if it occurs and remove the CheckConnectionState calls completely? The code would stay cleaner that way and should be more reliable that way.