Long time downloading several files

Feb 1, 2014 at 5:33 PM
Edited Feb 1, 2014 at 5:35 PM
Hi,

Managed to put together some code to download files via FTP, but are having a little trouble with the time as it takes very long to make the downloads, especially when downloading a couple of files at the same time. I can see in the log that my client sends a QUIT command after each download, and then has to negotiate user/passw again before next download, not sure if this is the issue. I'll post the logg and code below. Would be very glad for any help you can give in adjusting settings/code to speed things up!

I just copied the log for one downloadcycle, the following looks exactly the same:

PWD
257 "/out" is the HFS working directory.
InterNetwork: 123.456.789.111
220-FTP 18:58:26 on 2014-02-01.
220 Connection will close if idle for more than 30 minutes.
AUTH TLS
234 Security environment established - ready for negotiation
Time to activate encryption: 0h 0m 0s, Total Seconds: 0,1350927.
USER <username>
331 Send password please.
PASS <omitted>
230 <username> is logged on. Working directory is "/out".
PBSZ 0
200 Protection buffer size accepted
PROT P
200 Data connection protection set to private
Text encoding: System.Text.ASCIIEncoding
PWD
257 "/out" is the HFS working directory.
CWD /out
250 HFS directory /out is the current working directory
TYPE A
200 Representation type is Ascii NonPrint
SIZE /out/UPDR_CSV_20140111_020503.TXT
550 command SIZE is not supported for the current data connection translate table when type is A
PASV
227 Entering Passive Mode (123,456,789,12,45)
InterNetwork: 123.456.789.111
RETR /out/UPDR_CSV_20140111_020503.TXT
125 Sending data set /out/UPDR_CSV_20140111_020503.TXT
Time to activate encryption: 0h 0m 0s, Total Seconds: 0,031293.
250 Transfer completed successfully.
QUIT
221 Quit command received. Goodbye.
Disposing FtpClient object...
Disposing FtpSocketStream...
Disposing FtpSocketStream...

The part of the code for downloading looks like this (yes, I am a beginner!):
 FtpListItem[] items = länk.GetListing();

                int x = items.Count();

                for (int i = 0; i < x; i++)
                {

                  
                    if (items[i].Type == FtpFileSystemObjectType.File)
                    {

                        try
                        {


                            using (Stream istream = länk.OpenRead(länk.GetWorkingDirectory() +"/" + items[i].Name.ToString(), FtpDataType.ASCII))  
                            {
                                FileStream fileoutput = new FileStream(WindowsFormsApplication1.Properties.Settings.Default.filväghämta + "/" + FilnamnHämta.filNamn(items[i].Name.ToString())   , FileMode.Create, FileAccess.Write); 

                                try
                                {
                                    byte[] buffer = new byte[256];
                                    int len;

                                    while ((len = istream.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        fileoutput.Write(buffer, 0, buffer.Length);

                                    }
                                }
                                catch (Exception ex)
                                {

                                    MessageBox.Show(ex.Message);

                                }


                                istream.Close();
                                fileoutput.Close();

                                



                            }
Thanks in advance for any help I can get!

Regards,

Melmen

Sorry forgot to mention that each file is 1kB in size!
Coordinator
Feb 1, 2014 at 5:37 PM
You can set the EnableThreadSafeDataConnections property = false which will stop it from opening a new connection for file transfers. The caveat is that you cannot do multiple transfers from a single FtpClient object, you can do 1 transfer at a time. The protocol does not support doing multiple transfers over a single control connection which is why a new connection is created per transfer. This will help speed things up marginally depending on the speed of the connection. Just keep in mind that you cannot call OpenRead() or OpenWrite() until any existing transfer has completed. If you want to do multiple downloads you need to create an instance of the FtpClient class for each transfer.
Feb 1, 2014 at 5:52 PM
Thanks for the (very) fast reply! So if I understand you correctly it is possible to make multiple, consecutive transfers without closing/opening the connection, from a single FtpClient object. Will try this and report back how it went.

Thanks again!
Coordinator
Feb 1, 2014 at 5:58 PM
It is not possible to do multiple transfers from a single FtpClient instance unless EnableThreadSafeDataConnections = true. What that property does is tell FtpClient to clone itself and open a new connection for the file transfer. That is the only possible way to transfer multiple files simultaneously; establish a new connection to the server. It's not a limitation of FtpClient, it's a limitation of the FTP protocol.