0 votes
by (160 points)
edited

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 (http://forum.rebex.net/questions/1476/why-is-checkconnectionstate-not-throwing-an-exception-when-the-connection-has-been-closed-by-the-server) 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
by (148k points)
edited

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.

...