0 votes
by (200 points)
edited

In our .net CF application we connect every 5 minutes to a FTP-server. The connection is made by 3G/GPRS, sometimes it happens that due to heavy network use the 3g network does not connect to the server. Is there some method to implement a timeout into the connect method.

At the moment I created the following solution, but i'm not sure if this is the right approach.

ConnCounter byte=0;

client.Connect("ftp.example.com");

ConnCounter=1; while ((client.State=FtpState.Connecting)&& (ConnCounter)<12))
{
  System.Threading.Thread.Sleep(5000);
  ConnCounter++;
}
if (client.State==FtpState.Ready)
{
     client.Login("user","password");
     //process dataexchange
}
Applies to: Rebex FTP/SSL

5 Answers

0 votes
by (148k points)
edited

The code won't work very well because the Connect method will only return after the connection has been successfully established, otherwise it will throw an exception.

However, there is actually a built-in timeout support for all Ftp methods including Connect. By default, the timeout is set to 60 seconds, but it can be changed. Try this code:

client.Timeout = 300 * 1000; // set timeout to 300 seconds (5 minutes)
client.Connect("ftp.example.com"); // connect to the FTP server
client.Login("user","password"); // log in

To handle connection error, put a try/catch (FtpException ex) block around this code and handle errors in the catch section.

0 votes
by (200 points)
edited

I have altered the code to

ConnError = false; client.Timeout = 10 * 1000; try { client.Connect(OConfig.GetSettingDefault("FTP/Host", "ftp.techcontrol.nl")); } catch (FtpException ex) { AppVars.CurrentCommState = "!Con"; ConnError = true; MessageBox.Show(ex.Message); } if (!ConnError) { try { client.Login("user","password") } finally { } }

as the FTP-server address I have entered an invalid/unreachable server. But no exception is being thrown. Can it be that this has to do with the fact I'm using this code in a BackgroundWorker? (OpenNetCF.ComponentModel.BackgroundWorker)

by (148k points)
edited

We are not familiar with OpenNetCF's BackgroundWorker, but I would expect the message should be displayed. To see what is actually going on during the Connect method and whether it really times out, try using Ftp object's LogWriter property to create a log, as described at http://www.rebex.net/kb/logging.aspx (make sure to change the log path to a -based path that is valid in the CF device) and post the log here or mail it to support@rebex.net - we should then be able to tell what is going on!

0 votes
by (200 points)
edited

Thanks to the logWriter option I have found out that there was a successful connect, the problem was an incorrect errorhandling at the login method. Also an invalid FTP-server address was used, therefor is was possible to connect to this server. I have tested it with are unreachable IP(not dns name), now there is a proper time-out notification when the server is not responding.

Thanks for this good support!

0 votes
by (140 points)
edited

Hi, I have the following code and keep getting an exception that says 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.'. If I ender my ftp...file.zip into a browser it connects after a minute or so and starts downloading a large file. Any ideas?

Ftp ftp = new Ftp();
ftp.Timeout = 300 * 1000; //5 minutes

ftp.Settings.SslAllowedSuites = TlsCipherSuite.Secure;
ftp.Settings.SslAllowedVersions = TlsVersion.SSL30 | TlsVersion.TLS10;
ftp.Connect("ftp://...file.zip", SslMode.Implicit);

Thanks

0 votes
by (73.6k points)
edited

Please note that browser doesn't use SSL, so equivalent code should be:

ftp.Connect("ftp://...file.zip", SslMode.None);

However, if your server has SSL enabled, you can try SslMode.Explicit instead of SslMode.Implicit.

...