Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FTP download speed issue: socket programming vs 1

Status
Not open for further replies.

yezu1

Programmer
Jan 24, 2011
2
0
0
GB
Hi there,

I'm trying to write a simple c# application which downloads a large number of small files from an FTP server.
I've tried two approaches:
1 - generic socket programming
2 - using FtpWebRequest and FtpWebResponse objects

The download speed (for the same file) when using the first approach varies from 1.5s to 7s, the 2nd gives more less the same results - about 2.5s each time.

Considering that about 1.4s out of those 2.5s takes the process of initiating the FtpWebRequest object (only 1.1s for receiving data) the difference is quite significant.

The question is how to achieve for the 1st approach the same good stable download speed as for the 2nd one?

For the 1st approach the problem seems to lay in the loop below (as it takes about 90% of the download time):

Int32 intResponseLength = dataSocket.Receive(buffer, intBufferSize, SocketFlags.None);
while (intResponseLength != 0)
{
localFile.Write(buffer, 0, intResponseLength);
intResponseLength = dataSocket.Receive(buffer, intBufferSize, SocketFlags.None);
}

Equivalent part of code for the 2nd approach (always takes about 1.1s for particular file):

Int32 intResponseLength = ftpStream.Read(buffer, 0, intBufferSize);
while (intResponseLength != 0)
{
localFile.Write (buffer, 0, intResponseLength);
intResponseLength = ftpStream.Read(buffer, 0, intBufferSize);
}

Thanks in advance!
 
Is it a buffering issue? Perhaps the stream reader assembles the chunks asynchronously in the background so that the next one is already waiting when you make the next call? Perhaps it might be worth experimenting with buffer sizes. I'm guessing wildly here...
 
Thanks for your suggestion - I've tried buffers from 56b to 32kB - no significant difference.

Also creating a stream on the open data socket:

Stream str = new NetworkStream(dataSocket);

and reading it (instead of using dataSocket.Receive)

str.Read(buffer, 0, intBufferSize);

doesn't help... in fact it's even slower.

It must be something about data socket I guess...

cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top