0 votes
by (140 points)
edited

Hi, I'm writing a simple program that are downloading several files at the same time in different threads and It seems that as soon as the first file is finished all other file downloads ends with this exception: error in thread = 4 filename = movie1.wmv, error = An existing connection was forcibly closed by the remote host System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host.

It works fine before the first file is ready and I have five files downloading at same time.

Anyone have an idea what the problem can be? the server being used is Titan.

The code looks like this when starting the download for every file.

try
{
    Ftp client = new Ftp();
    client.Connect(hostname, port);
    client.Login(username, password);
    long fileSize = client.GetFile(fileName, fileNameWithPath);
}
catch (Exception ex)
{
    ...
}
Applies to: Rebex FTP/SSL

1 Answer

0 votes
by (13.0k points)
edited

Hi,

the FTP protocol does not support multiple file transfers in the same time.

If you want to download several files at once you have to maintaint multiple connections to the FTP server. This can cause problems with some FTP servers because they like to limit number of concurent connections from one user or from one IP address. The most common limit is 2 concurent transfers per user.

Pseudo code for downloading the file in two threads would look like this:

FileNameBuffer buffer = GetFileListFromFtpServer();
StartFirstDownloadThread(files);
StartSecondDownloadThread(files);

Pseudo code for each download thread would be:

ConnectToFtp();
while (file = GetNextFileFromBuffer())
{
   DownloadFile();
}
Disconnect();

You can download the fully functional code for downloading all files from FTP folder in two threads from http://www.rebex.net/extras/FtpsMultiDownloader.zip

If you want to include a support for simulataneous transfers directly to the FTP component please consider voting on the following feature requests:

by (140 points)
Hi and thanks for your answer. The thing is that I do it like in your example and it works sometimes. It can start up five downloads at the same time and they download without any problems. The problem is that sometimes(and usually when the first file is fully downloaded) the rest of the files disconnects as well(and the strange thing is that it doesn't happen all the times?). Downloading against same ftp server with filezilla works fine without a problem.
by (13.0k points)
...