ArxOne / FTP

Simple FTP client
MIT License
37 stars 15 forks source link

library hits session limit on server where filezilla does not. #50

Closed bzuidgeest closed 5 years ago

bzuidgeest commented 5 years ago

Hello,

I'm having a problem where I have to download several files from an external ftp server (of which I have little knowledge or control). Using filezilla everything works using the same settings. But when I use this library and the code below, I can only download the first three files and then it stops with authentication errors. After that filezilla also won't work, but it gives an more specific error. Something like "maximum number of logins reached". I contacted the server owner and they increased the max connection limit. But that only helps so far.

What I really want to know is why filezilla is not having a problem (when starting with no outstanding sessions) and this library does (for the same number of files, both passive connections). Other projects I used it on work fine, so I guess it's something to do with the specific server I am connecting to.

Is there something in the code below that I can change to better "close" the connection or something after a download? Maybe I'm just missing something. Any help appreciated.

`using (var ftpClient = new FtpClient(new Uri("ftps://123.123.123.123"), credential, new FtpClientParameters() { ConnectTimeout = new TimeSpan(0, 0, 10), Passive = true })) { if (ftpClient.ServerFeatures.HasFeature("MLSD")) { entries = ftpClient.MlsdEntries(myPath); } else { entries = ftpClient.ListEntries(myPath); }

            foreach (FtpEntry entry in entries)
            {
                ftpClient.Retr(entry.Path, FtpTransferMode.Binary);
            }
        }`
picrap commented 5 years ago

Hi,

first of all, the FtpSession.Retr method returns a Stream that you must read and copy to a local file. What it does here is nothing. So second part, I guess there is a limit of 3 simulateneous open files per client, but if you actually read, copy and close each returned Stream, it will work just fine.

Let me know!

bzuidgeest commented 5 years ago

Hello,

Me not reading the stream in my example was because I took that code out to simplify. I was using the code in a Microsoft SQL CLR Scalar function and there the stream gets handed over to SQL itself for processing. Your second part was the real trigger for me. Read copy AND close. Apparently the stream does not get closed properly (or directly) when handed over to SQL, but I never considered that. And when I tested the code from a console program I forgot to add in reading and closing because it was not there in the original code. In short, I now copy the stream to a memorystream, close the original stream and hand the memorystream to SQL for processing. Problem fixed. To bad the compiler does not error out when you forget to close streams, but one cannot have everything :)

Final note, your library was one of the few if only ftp libraries I could find without extra dependencies aside from basic .net framework stuff. Really useful as loading extra dependencies in the SQL CLR is always somewhat troublesome. Straight forward and simple to use. Thank you for sharing it.