リモートサービスプロバイダから〜40MBのCSVファイルをFTPするには、次のC#コードを使用しています。約50%の時間で、ダウンロードが中断し、最終的にタイムアウトします。私はLeechFTPのようなグラフィカルなクライアントを使用して対話形式でファイルをダウンロードすると、ダウンロードがハングアップしないと、約45秒で完全にほとんどないC#アプリケーションでFTPタイムアウトを解決する方法
> 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.
:私のアプリのログでは、私のような行を取得します。私は間違っていることを理解する時間を持っています。
このコードをどのように計測して、何が起こっているのか、このファイルをダウンロードするためのより良い方法を知ることができますか?私はバッファサイズを増やすべきですか?どれくらい?ディスクへのバッファリングされた書き込みを避け、メモリ内のファイル全体を呑み込もうとしますか?どんなアドバイスもありがとう!
...
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;
}
うん、良いアイデア - それは根本的な問題を解決するのではなく、むしろ働きます。グラフィカルなFTPクライアントはめったにタイムアウトしません。なぜ私のコードはそうするのですか? –