Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to read stream in parallel from FTP server but i getting only two streams read at a time

C#
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
Posted
Updated 8-Mar-11 23:17pm
v2
Comments
m@dhu 9-Mar-11 5:17am    
Use pre tags which makes code readable.
Debojyoti Majumder 9-Mar-11 5:59am    
use thread...

1 solution

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


or change the default settings for all new ServicePoint objects:
ServicePointManager[^]
ServicePointManager.DefaultConnectionLimit = 10 // number of connections


Hope this help.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 9-Mar-11 15:11pm    
Good, my 5.
However, you should not call "limitation of .NET" (I was surprised: where is the limitation?) when this is no more but some default.
--SA
TimGameDev 9-Mar-11 16:14pm    
Sorry, of course I mean default connection limit. That is my english :) Thanks for correction. Answer is improved!
Sergey Alexandrovich Kryukov 9-Mar-11 18:42pm    
Good, thank you. (Oh, my vote was 5 already :-)
--SA
kalaiy2 14-Mar-11 10:41am    
thanks Timur its working

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