我想从FTP并行读取流 [英] I want to read stream in parallel from FTP

查看:83
本文介绍了我想从FTP并行读取流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从FTP服务器并行读取流,但一次只能读取两个流

I want to read stream in parallel from FTP server but i getting only two streams read at a time

Parallel.For(0, dt.Rows.Count, i =>

{

downloadFile();

});

public void downloadFile(long StartPoint, long EndPoint, string FtpAddr, string DfileName, int i)
{
string FTPAddress = @"ftp:\\" + FtpAddr;
string filename = DfileName;
string Partfilename = Path.GetFileName(DfileName);
long length = EndPoint - StartPoint;
try{
Stream strLocal;
WebProxy wp = new WebProxy();
wp.UseDefaultCredentials = true;//Create FTP request 
FtpWebRequest request = FtpWebRequest.Create(FTPAddress) as FtpWebRequest;
request.Proxy = wp;             
request = FtpWebRequest.Create(FTPAddress) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Proxy = wp;
request.UsePassive = false;
request.UseBinary = true;
request.KeepAlive = true; //close the connection when done
request.ContentOffset = StartPoint;
Stream reader = request.GetResponse().GetResponseStream();
byte[] buffer = new byte[1048576]; //downloads in chuncks
if (!File.Exists(filename))
{
strLocal = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
}
else
{
strLocal = new FileStream(filename, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
}
int bytesSize = 0;
while ((bytesSize = reader.Read(buffer, 0, buffer.Length)) > 0)//in this line only two streams are readed at a time
{    
if ((strLocal.Length + bytesSize) < length)
{
strLocal.Write(buffer, 0, bytesSize);
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, length, i, Partfilename, bytesSize });
}
else
{
long Valtowrite = (strLocal.Length + bytesSize) - length;
Valtowrite = bytesSize - Valtowrite;
strLocal.Write(buffer, 0, (int)Valtowrite);
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, length, i, Partfilename, bytesSize });
break;
}
}
strLocal.Close();                
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "There was an error connecting to the FTP Server.");
}
}



请帮助我紧急

在此先感谢



please help me its urgent

Thanks in Advance

推荐答案

.NET应用程序在同一时间点的默认连接限制为2.
但是,使用以下类可以更改此设置:
ServicePoint类 [
Default connection limit to a point at the same time for .NET applications is 2.
But using the following class you can change this setting:
ServicePoint Class[^]

In your case you should do something like this:
request.ServicePoint.ConnectionLimit = 10 // number of connections



或更改所有新的ServicePoint对象的默认设置:
ServicePointManager [



or change the default settings for all new ServicePoint objects:
ServicePointManager[^]

ServicePointManager.DefaultConnectionLimit = 10 // number of connections



希望有帮助.



Hope this help.


这篇关于我想从FTP并行读取流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆