Click here to Skip to main content
15,904,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So... I have this program that has been working for some years now. I connect to an ftp server, list the files in the directory, check if they already exist in my local directory and then downloads the ones who doesn't.

Suddenly, a few weeks ago it stopped downloading files. It couldn't connect to ftp server. Around the same time the network administrators changed some routers and did some maintenance. According to them there is no firewall or virus program stopping the connection.

I use FtpClient.cs : This is part of my code :

C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = (BackgroundWorker)sender;
    SqlConnection con = new SqlConnection(ConString);
    con.Open();
    if (FbackgroundWorker1.CancellationPending) { e.Cancel = true; return; }
    foreach (DataRow ship in s_dt.Rows)
    {
        worker.ReportProgress(2100, ship["name"].ToString());
        FtpClient ftpc = new FtpClient(ship["FTP"].ToString().Trim(), userName, userPassword, timeOut, ftpPort);
        try
        {
            worker.ReportProgress(1000, "Login to FTP Server on " + ship["name"].ToString().Trim());
            ftpc.Login();
            if (FbackgroundWorker1.CancellationPending) { e.Cancel = true; ftpc.Close(); return; }
            worker.ReportProgress(1000, "Change Directory to: " + pathName);
            ftpc.ChangeDir(pathName);
            if (FbackgroundWorker1.CancellationPending) { e.Cancel = true; ftpc.Close(); return; }
            worker.ReportProgress(1000, "Download file list");
            string[] listOfFiles = ftpc.GetFileList();
            if (FbackgroundWorker1.CancellationPending) { e.Cancel = true; ftpc.Close(); return; }
            worker.ReportProgress(2000, listOfFiles);
            ftpc.Close();
            worker.ReportProgress(1000, "Close connect to FTP Server on " + ship["name"].ToString().Trim());
            worker.ReportProgress(1000, "Select new files");
            string[] LMF = myCheckList(listOfFiles, ship["id"].ToString().Trim());
            worker.ReportProgress(2001, LMF);
            int numberOfFiles = LMF.Count();
            worker.ReportProgress(3000, numberOfFiles);
            int ret = 0;
            worker.ReportProgress(1000, "Login to FTP Server");
            ftpc.Login();
            if (FbackgroundWorker1.CancellationPending) { e.Cancel = true; ftpc.Close(); return; }
            foreach (string zbior in LMF)
            {
                worker.ReportProgress(2101, zbior);
                worker.ReportProgress(1000, "Change Directory to: " + pathName + @"\" + zbior);
                ftpc.ChangeDir(pathName);
                ftpc.ChangeDir(zbior);
                if (FbackgroundWorker1.CancellationPending) { e.Cancel = true; ftpc.Close(); return; }
                TimeSpan ts = DateTime.UtcNow - new DateTime(2008, 1, 1, 0, 0, 0, 0);
                string timeStamp = Convert.ToInt64(ts.TotalMilliseconds).ToString();
                string fullFileName = impPathName + @"\ship" + ship["id"] + @"\TREND24(F)(fileID" + timeStamp + @")(" + zbior + @").SKV";
                worker.ReportProgress(1000, "Download file: " + fullFileName);
                ftpc.Download("TREND24.SKV", fullFileName, true);
                worker.ReportProgress(1000, "Test file: " + zbior);
                StreamReader sr = new StreamReader(fullFileName);
                string fileToTest = sr.ReadToEnd();
                if (fileToTest.Contains("END"))
                {
                    SqlCommand command = new SqlCommand("INSERT INTO [dbo].[datafiles](ship_id, fileName, dY, dM, dD, [DownloadDate], [ident] ) VALUES ("
                        + ship["id"] + ", '"
                        + zbior + "', "
                        + zbior.Substring(0, 2) + ", "
                        + zbior.Substring(2, 2) + ", "
                        + zbior.Substring(4, 2) + ", '"
                        + DateTime.Now.ToString(dateTimeFormat) + "', '"
                        + timeStamp + "' )", con);
                    ret = ret + command.ExecuteNonQuery();
                    FbackgroundWorker1.ReportProgress(ret);
                    worker.ReportProgress(1000, "Test OK");
                }
                else
                {
                    worker.ReportProgress(1000, "file: " + zbior + " not contain END");
                }
            }
            if (FbackgroundWorker1.CancellationPending) { e.Cancel = true; ftpc.Close(); return; }
            worker.ReportProgress(1000, "Close connect to FTP");
            worker.ReportProgress(2102, "Clear file list");
            ftpc.Close();
        }
        catch (Exception ex)
        {
            ftpc.Close();
            worker.ReportProgress(1000, "Close connect to FTP error: " + ex.Message);

        }
        finally
        {
            worker.ReportProgress(1000, "Problem with connect to FTP ");
        }
    }
    if (con != null) con.Close();

}


I ran breakpoints while debugging and this is the errors i have:

”An established connection was aborted by the software in your host machine”, with ErrorCode 10053

and

clientSocket.EnableBroadcast threw an exception of type System.Net.Sockets.SocketEcxeption. ”An unknown, invalid or unsupported option or level was specified in a getsockopt or setsockopt call”. ErrorCode 10042. SocketErrorCode: ProtocolOption.

and

clientSocket.MulticastLoopback threw an exception of type System.Net.Sockets.SocketEcxeption. ”An invalid argument was supplied”. NativeErrorCode 10022

Need help !

thanks
Posted
Comments
ZurdoDev 27-Feb-14 16:31pm    
Still sounds to me like it's a setup issue on the ftp end.
Handyman83 27-Feb-14 16:38pm    
I know... The funny thing is that it has been working for 2 years, every day. No changes to the code! Don't know what to do
ZurdoDev 27-Feb-14 16:47pm    
Tell your IT to fix it. :)
Sergey Alexandrovich Kryukov 27-Feb-14 16:39pm    
That is easy to check up using one of the available FTP clients...
—SA
Handyman83 27-Feb-14 17:13pm    
I have access with filezilla ftpclient, but no access with smartFTP ! I even wrote a new code using ftpwebrequest and the problem was the same. I have noe problem finding the files in the directory, but when i try to download these errors appear.

1 solution

Here is part of the FtpClient.cs code:
it seems as I have an error in : addr = Dns.GetHostEntry(this.server).AddressList[0];
"Index was outside the bounds of the array"
C#
public void Login()
		{
			if ( this.loggedin ) this.Close();

			Debug.WriteLine("Opening connection to " + this.server, "FtpClient" );

			IPAddress addr = null;
			IPEndPoint ep = null;

			try
			{
				this.clientSocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
				//addr = Dns.Resolve(this.server).AddressList[0];
                addr = Dns.GetHostEntry(this.server).AddressList[0];
				ep = new IPEndPoint( addr, this.port );
				this.clientSocket.Connect(ep);
			}
			catch(Exception ex)
			{
				// doubtfull
				if ( this.clientSocket != null && this.clientSocket.Connected ) this.clientSocket.Close();
                try
                {
                    throw new FtpException("Couldn't connect to remote server", ex);
                }
                catch { }
			}

			this.readResponse();

			if(this.resultCode != 220)
			{
				this.Close();
				throw new FtpException(this.result.Substring(4));
			}

			this.sendCommand( "USER " + username );

			if( !(this.resultCode == 331 || this.resultCode == 230) )
			{
				this.cleanup();
				throw new FtpException(this.result.Substring(4));
			}

			if( this.resultCode != 230 )
			{
				this.sendCommand( "PASS " + password );

				if( !(this.resultCode == 230 || this.resultCode == 202) )
				{
					this.cleanup();
					throw new FtpException(this.result.Substring(4));
				}
			}

			this.loggedin = true;

			Debug.WriteLine( "Connected to " + this.server, "FtpClient" );

			this.ChangeDir(this.remotePath);
		}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900