0 votes
by (120 points)
edited

Rebex FTP doesn't seem to be able to detect when the FTP server is no longer connected.

A couple of days ago I downloaded Rebex.Net.Ftp version 3.0.3854.0 for use with Visual Studio 2008 VB.

Using FileZilla Server 0.9.37, I connect to the FTP server thusly:

    client.Connect(txtFtpHostOrIP.Text, txtPort.Text)
    client.Login(txtUserName.Text, txtPassword.Text)

I can successfully traverse the directory structure and list directory contents.

I wanted to test my ability to detect when the FTP connection times out and is no longer connected. I attached the following code to a button. Clicking on the button before connecting results in "Not Connected" being displayed. Clicking on the button after connecting results in "Connected" being displayed.

However, if I:

  1. Let the connection time out.
  2. Click on the button - results in "Connected" being displayed, even though I know it's not connected.
  3. Trying to change directories at this point results in the following error:

    FtpExceptin was unhandled. An established connection was aborted by the software in your host machine.

Any idea why the .GetConnectionState.Connected is not detecting that things are no longer connected? I don't want to use try/catch to determine when I don't have a connection, I want to use GetConnectionState.Connected.

Thanks,

Jim

Private Sub butDisplayState_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butViewAsHL7.Click

    If client.GetConnectionState.Connected Then
        Me.Text = "Connected"
    Else
        Me.Text = "Not Connected"
    End If

End Sub
Applies to: Rebex FTP/SSL

1 Answer

0 votes
by (147k points)
edited

Ftp object's GetConnectionState method returns "Connected" if the connection is not currently known to have ended. If the connection was not closed yet by either the client or the server but it's already broken, GetConnectionState will return "Connected" because the local machine is not yet aware that the connection has been lost.

The only 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 become "Not Connected" as soon as an error occurs. However, sending a command to the FTP server and waiting for failure or response is not as simple as checking the current state of an TCP socket (which is what `GetConnectionState does) and may take many seconds to complete.

A more reliable way to detect whether the connection is still active would be to call Ftp object's KeepAlive method or GetCurrentDirectory method inside a try/catch block and if FtpException occurs, check its Status property.

We might add an overload for GetConnectionState in one of the future releases that would do this automatically, but its behavior would be equivalent to what you get by calling KeepAlive or GetCurrentDirectory and catch FtpException.

...