+1 vote

Do you have any asynchronous examples when using the FTP client?

It seems every example and even questions asked here are all using a synchronous connection which is fine, but this seems wasteful having to create a new connection if my application is idling for a bit and the server has not kicked me.
I see your help file does show this can be done but I'm a bit lost making sense of it, so a sample would be great if possible.

public static Ftp ftpClient;

public static Ftp FTPConnect(FTPDetails details)
{
    try
    {
        Limilabs.FTP.Log.Enabled = true;
        using (Ftp client = new Ftp())
        {
            client.Connect(details.hostname, details.port);
            client.AuthTLS();
            client.IgnorePassiveModeAddress = true;
            client.Login(details.username, details.password);
            ftpClient = client;

            // This returns true when called here, great.
            var test = IsFtpConnected(); 

            return client; 
        }
    } 
    catch (Exception ex)
    {
        ftpClient = null; 
        return null;
    }
}

// if called from the main thread, always returns false. 

public static bool IsFtpConnected() 
{
    if (ftpClient.Connected)
    {
        return true;
    }
    return false;
}
by (260 points)

1 Answer

+1 vote

Ftp class represents a single control connection to the FTP server.
Upload and Download methods create a data connection and transfer appropriate data to od from the server.

You are free to use 'Ftp' instance as long as you want, however you need to understand, that most servers will cut your connection after some inactivity period (the timeout is usually set to couple of minutes, some routers may be even more aggressive). This will result in an exception on the subsequent operation.

Looking at your code, the problem is with you using an using clause:

using (Ftp client = new Ftp())

...it disposes the Ftp instance and closes the underlying connection as soon as it exits FTPConnect method.

by (301k points)
edited by
Well I can't get this to work. The server in question is my own where I am looking into updating a small sqlite db regularly.
The idea was to have a class helper with a couple of methods to handle things like connecting, checking the connection with ftp.Connected, and uploading files.

However the instance always reports back ftp.connected as false the moment I return from the connection method that handles the connection. The server also confirms this, and the connection is dropped suddenly, not as would be expected with ftp.close.
If I handle everything in a single method (much like the samples here) there is no problem. Something seems to kill the instance for me and I'm not sure what to look at.
The problem might be with your code or your server implementation. Does it work without checking Connected property?
I can cross the server out being a problem. But you could be right it may be with my code, I'm not particular good with c# but let me throw together an example that causes the problem and share.
I'm not sure how to mark this as code but here is an example:
If IsFtpConnected is called after the FTPConnect method, it's always false. If it's inside the FTPConnect method, it's true.
It's probably something I've done.
You are using an `using` clause:

using (Ftp client = new Ftp())

...it disposes the `Ftp` instance and closes the underlying connection.
ah wow, such a simply problem at my end!.
Thanks so much for that!! This library is looking very promising for me!
Ok now I have the opposite problem.
It seems that ftp.Connected still returns true even if the user/instance is kicked off the server. I understand why this is happening actually.
So the best way to add a method to check the connection state would be to send a FTP command and see if there is an exception.
...