Page 1 of 1

FTP connection failing with “Unable to connect to the remote server”

Posted: 2019-10-15 07:56
by nkr_
Hello,

I am experiencing some issues in my .NET Core 2.2 setup where I have a feature that needs to process a lot of files located on our VM. To retrieve the files, I make continous ftpwebrequests, with FileZilla as our FTP Server running on the VM.
The issue comes when I need to download hundreds of files using the FTPWebrequests, somewhere random between 300-400 requests, I get the error "Unable to connect to the remote server".

My code for retrieving the files looks like this:

Code: Select all

	    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(filepath);
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            ftpRequest.Credentials = new NetworkCredential("username", "password");
            
            if (finalProcessRequest)
            {
                ftpRequest.KeepAlive = false;
            }
            else
            {
                ftpRequest.KeepAlive = true;
            }
            
            Encoding encoding = Encoding.GetEncoding(Settings.StandardCodingPage);

            if (useUTF8)
            {
                encoding = Encoding.UTF8;
            }

            if (codePage != -1)
            {
                encoding = Encoding.GetEncoding(codePage);
            }

            StringBuilder objectCode = new StringBuilder();

            FtpWebResponse webResponse = (FtpWebResponse) ftpRequest.GetResponse();
            Stream responseStream = webResponse.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream, encoding);

            string line;
            while ((line = await reader.ReadLineAsync()) != null)
            {
                objectCode.AppendLine(line);
            }

            reader.Close();
            webResponse.Close();

            result = objectCode.ToString();

Re: FTP connection failing with “Unable to connect to the remote server”

Posted: 2019-10-15 11:09
by botg
Do you still experience this issue after switching to a non-garbage-collected language?

Re: FTP connection failing with “Unable to connect to the remote server”

Posted: 2019-10-15 11:55
by nkr_
Hi botg,

I don't know if it would work in a non-garbage-collected language, and I need it to work for this setup. There's no indicator to what is causing it, it just happens randomly in the request range of 300 to 400, I never get past that amount of requests (Total download requests I need to make is 1300 ish), the files themselves aren't more than 20 to 350 KB in size.

Please help.

Thanks,
Nikolaj.

Re: FTP connection failing with “Unable to connect to the remote server”

Posted: 2019-10-18 09:02
by nkr_
I tried different approaches and looked for optimal settings to set in the FileZilla Server, but I haven't been able to get past the issue yet and could really use some help.

Re: FTP connection failing with “Unable to connect to the remote server”

Posted: 2019-10-18 12:34
by nkr_
Update:

I have modified the code to run the file processing in parallel, instead of a normal loop.
Besides making it run in parallel, I added try/catch with a sleep timer and repeat cycle on catches - this takes care of most of the processes hitting "unable to connect to remote".

Further more, I increased the number of threads and buffer sizes in my FileZilla Server, took some time adjusting and testing to find what worked best for my setup, but overall, everything is getting smooth now and out of roughly 2100 file download requests over ftp, only 2-3 (random) are hitting "unable to connect to remote" error.

Re: FTP connection failing with “Unable to connect to the remote server”

Posted: 2019-10-18 21:41
by boco
The occasional error messages are normal for FTP due to its nature of using many ports instead of only one. There's always the change of occasionally hitting a socket that cannot serve the request at that very moment. FileZilla is meant to mitigate these issues, it will simply retry and the server will use a different socket that hopefully works, then.

Re: FTP connection failing with “Unable to connect to the remote server”

Posted: 2020-02-11 14:35
by BarryMB
Dear All,
Could some one help me to fix this pbm.
Since this morning, I have the same error.
Thx for any help