C# FTP really slow?!

Soldato
Joined
26 Aug 2006
Posts
9,726
Location
62.156684,-49.781113
I'm trying to get an FTP transfer in code to speed test something, but for some reason no matter how I try, it ends up taking twice as long as if I do it through console?

I'm transferring a 100MB file, here's the result of the console transfer:

Code:
ftp> put FTP_UP.txt
200 Port command successful
150 Opening data channel for file transfer.
226 Transfer OK
ftp: 104857600 bytes sent in 40.57Seconds 2584.42Kbytes/sec.
ftp>

However, the two methods I've tried - raw C# first and using a library (http://www.enterprisedt.com/products/edtftpj/) (second) take 70+ seconds...

Code:
public static TimeSpan PutFile(Uri serverUri, string LocalFile, string Username, string Password)
{
	FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
	request.Method = WebRequestMethods.Ftp.UploadFile;
	request.KeepAlive = false;

	request.Credentials = new NetworkCredential(Username, Password);

	DateTime timeStart = DateTime.Now;
	Stream requestStream = request.GetRequestStream();

	const int BUFFERSIZE = 4096;
	byte[] buffer = new byte[BUFFERSIZE];
	FileInfo uploadfile = new FileInfo(LocalFile);
	FileStream fs = uploadfile.OpenRead();
	while ((fs.Read(buffer, 0, BUFFERSIZE)) != 0)
	{
		requestStream.Write(buffer, 0, BUFFERSIZE);
	}
	requestStream.Close();
	fs.Close();
	DateTime timeStop = DateTime.Now;

	FtpWebResponse response = (FtpWebResponse)request.GetResponse();

	response.Close();

	return timeStop - timeStart;
}


Code:
public static TimeSpan PutFile(IPAddress serverUri, string LocalFile, string Username, string Password)
{
	FTPClient ftp = null;

	ftp = new FTPClient(serverUri);

	ftp.Login(Username, Password);

	ftp.ConnectMode = FTPConnectMode.ACTIVE;
	ftp.TransferType = FTPTransferType.BINARY;

	DateTime timeStart = DateTime.Now;
	ftp.Put(LocalFile, "FTP_UP.txt");
	DateTime timeStop = DateTime.Now;

	ftp.Quit();

	return timeStop - timeStart;
}

Anybody point out what I'm doing wrong?

EDIT: Changing between active/passive and binary/ASCII didn't make any difference. The file is just a large file of emptiness created using "fsutil file createnew".
 
Last edited:
Have you tried doing multiple uploads in one execution of the C# program?

I know it's not the same, but I did a Web services project in C# and for some reason the first call to a Web service would always take ages, after that it was fine. It turned out that the .NET framework does some compiling (or something else) on the first time you call a Web service and that takes time. Perhaps a similar thing happening here?
 
Know what you mean with the web services stuff - that wouldn't necessarily explain the use of the external library though. Will give that a bash as soon as I can anyway.
 
For reference, it was about just making the loop quicker I think:

Code:
FileStream textFile = new FileStream(LocalFile, FileMode.Open);
byte[] buffer = new byte[textFile.Length];

DateTime timeStart = DateTime.Now;

using (Stream stream = request.GetRequestStream())
{
	stream.Write(buffer, 0, buffer.Length);
}

DateTime timeStop = DateTime.Now;

It's now comparable to running it through console.
 
Back
Top Bottom