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
}

asked 18 Jul '11, 16:23

Maneffic's gravatar image

Maneffic
15
accept rate: 0%

edited 05 Dec '11, 23:30

Martin%20Vobr's gravatar image

Martin Vobr ♦♦
335310


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.

link

answered 18 Jul '11, 16:37

Lukas%20Pokorny's gravatar image

Lukas Pokorny ♦♦
2.4k28
accept rate: 31%

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)

link

answered 18 Jul '11, 17:27

Maneffic's gravatar image

Maneffic
15
accept rate: 0%

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!

(18 Jul '11, 17:33) Lukas Pokorny ♦♦

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!

link

answered 18 Jul '11, 18:16

Maneffic's gravatar image

Maneffic
15
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×152
×20
×11

Asked: 18 Jul '11, 16:23

Seen: 518 times

Last updated: 05 Dec '11, 23:30