How to solve FTP timeouts in a C # application

I am using the following C # code for a 40 MB CSV FTP file from a remote service provider. In approximately 50% of cases, the download freezes and eventually ends. In my application log, I get a line like:

> Unable to read data from the transport
> connection: 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.

When I download a file interactively using a graphical client such as LeechFTP, the download almost never freezes and ends after about 45 seconds. I damn understand what is going wrong.

Can anyone suggest how I can use this code to get a better idea of ​​what is happening, or a better way to download this file? Should I increase the size of the buffer? How much? Avoid buffering disk writes and trying to internalize the entire file in memory? Any advice appreciated!

...

private void CreateDownloadFile()
    {
        _OutputFile = new FileStream(_SourceFile, FileMode.Create);
    }
public string FTPDownloadFile()
    {
        this.CreateDownloadFile();

        myReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(this.DownloadURI));
        myReq.Method = WebRequestMethods.Ftp.DownloadFile;
        myReq.UseBinary = true;
        myReq.Credentials = new NetworkCredential(_ID, _Password);

        FtpWebResponse myResp = (FtpWebResponse)myReq.GetResponse();
        Stream ftpStream = myResp.GetResponseStream();

        int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[bufferSize];
        int bytesRead = 0;
        readCount = ftpStream.Read(buffer, 0, bufferSize);

        while (readCount > 0)
        {
            _OutputFile.Write( buffer, 0, readCount );
            readCount = ftpStream.Read( buffer, 0, bufferSize );

            Console.Write( '.' );    // show progress on the console
            bytesRead += readCount;
        }
        Console.WriteLine();
        logger.logActivity( "    FTP received " + String.Format( "{0:0,0}", bytesRead ) + " bytes" );

        ftpStream.Close();
        _OutputFile.Close();
        myResp.Close();
        return this.GetFTPStatus();
    }

    public string GetFTPStatus()
    {
        return ((FtpWebResponse)myReq.GetResponse()).StatusDescription;
    }
+5
3

FTPClient, , -, FTPClient FtpWebRequest, - , .

, -1 -

, , .

:

// FTP-.

        FtpWebRequest reqFTP;

        string fileName = @"c:\downloadDir\localFileName.txt";
        FileInfo downloadFile = new FileInfo(fileName);
        string uri = "ftp://ftp.myftpsite.com/ftpDir/remoteFileName.txt";


        FileStream outputStream = new FileStream(fileName, FileMode.Append);

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.UseBinary = true;
        reqFTP.KeepAlive = false;
        reqFTP.Timeout = -1;
        reqFTP.UsePassive = true;
        reqFTP.Credentials = new NetworkCredential("userName", "passWord");
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        Stream ftpStream = response.GetResponseStream();
        long cl = response.ContentLength;
        int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[bufferSize];
        readCount = ftpStream.Read(buffer, 0, bufferSize);
        Console.WriteLine("Connected: Downloading File");
        while (readCount > 0)
        {
            outputStream.Write(buffer, 0, readCount);
            readCount = ftpStream.Read(buffer, 0, bufferSize);
            Console.WriteLine(readCount.ToString());
        }

        ftpStream.Close();
        outputStream.Close();
        response.Close();
        Console.WriteLine("Downloading Complete");
+2

FtpWebRequest FTP. FtpWebRequest - FTP API, .

FtpClient http://www.codeplex.com/ftpclient

IndySockets http://www.indyproject.org/index.en.aspx

+1

I did not do FTP at the code level, but FTP supports renewal. Perhaps you could automatically try to resume the download when it expires

0
source

All Articles