Why is CheckConnectionState not throwing an exception when the connection has been closed by the server.

0 votes
asked May 27, 2011 by John Hancock (260 points)
edited May 30, 2011

I'm having trouble determining when my connection has been closed by the ftp server.

Here's the test that I am doing (all on my local network):

  1. Start FTP server on machine A (Serv-U)
  2. Connect to server with machine B
  3. Manually close the connection using Serv-U
  4. Call CheckConnectionState to see if the connection is still good
  5. CheckConnectionState doesn't throw any exception and now I am in an invalid state

Am I misunderstanding what CheckConnectionState is supposed to do?

Applies to: Rebex FTP/SSL

1 Answer

0 votes
answered May 30, 2011 by Lukas Pokorny (128,290 points)
edited May 30, 2011

Ftp object's CheckConnectionState method only throws an exception if the connection is already known to have ended when the method is called. If the client machine is not yet aware that the connection has been lost, the method will return without raising an exception.

The only reliable way to determine whether the connection is still active is to send a command over it - if the connection has already been lost, it won't be possible to transmit the command to the server and the connection will be acknowledged as disconnected. In case of FTP, the KeepAlive or GetCurrentDirectory methods can be used for this purpose.

This said, it is a bit strange that CheckConnectionState can't detect a manually closed connection in this case. That kind of action should gracefully close the connection, making the client aware of it immediately, causing the CheckConnectionState method to fail. We will try to reproduce this problem ourselves. Until then, using one of the methods that actually send some command (see above) is recommended.