0

I just want to download 'n' files using ftp server at same time. My code is as follows...

Each time I run this code, only one file is getting downloaded and then raising an exception in GetResponse() line:

The remote server returned an error: (501) Syntax error in parameters or arguments.

class main{
public static void main(){ 
    Multiple_File_Downloader MFD= new Multiple_File_Downloader();
      MFD.Multi_Thread();  }
  }

    class Multiple_File_Downloader
    { 
      public void Multi_Thread()
        {
            Thread a = new Thread(new ThreadStart(() => Downloadfile("7.jpg")));
            Thread b = new Thread(new ThreadStart(() => Downloadfile("8.jpg")));

            a.IsBackground = true;
            b.IsBackground = true;

            a.Start();
            b.Start();
            }

        public void Downloadfile(string _filename)
        {
            string localPath = @"E:\FTPTrialPath\";

    FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create("ftp://url/" + _filename);
            requestFileDownload.Credentials = new NetworkCredential("Login","password");
            requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
            requestFileDownload.UsePassive =  true;

    using(FtpWebResponse  responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse()) //<<< ERROR HERE...
        {
                Stream responseStream = responseFileDownload.GetResponseStream();
                FileStream writeStream = new FileStream(localPath + _filename, FileMode.Create);

                int Length = 2048;
                Byte[] buffer = new Byte[Length];
                int bytesRead = responseStream.Read(buffer, 0, Length);

                while (bytesRead > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                    bytesRead = responseStream.Read(buffer, 0, Length);
                }
      }
                requestFileDownload = null;
      }
    }

Is it possible to do so without interfering the parameters of other thread? Thanks for the help in Advance :)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Dippy
  • 3
  • 3

2 Answers2

0

Each time you call a method it has its own set of parameters which is specific to that method call only.

Unless you

  • Pass something by reference (by using the ref keyword)
  • Pass a reference type (for example a class)
  • Pass a value type containing a reference type (i.e. a structure containing a class)
  • Modify a global variable (for example a class level variable)

...there will be no problem running the same method in multiple threads.

In your code you are creating two different strings which will only be accessible by the "method instance" that you passed it to.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • 1
    Even if you use a reference type, the parameter itself is independent of other calls... you just need to remember that the parameter value is only a reference. – Jon Skeet Sep 03 '16 at 12:10
  • @JonSkeet : That's what I meant, yes. I rephrased the answer as you made me realize I hadn't expressed it quite clearly. Thanks! – Visual Vincent Sep 03 '16 at 12:19
  • Well your answer now sounds like you believe passing a reference type by value is actually using pass by reference - it isn't, and it's best to avoid confusing the two... – Jon Skeet Sep 03 '16 at 12:23
  • @JonSkeet : Sorry, I'm not very used to explaining references and reference types. Better now? – Visual Vincent Sep 03 '16 at 12:30
  • 1
    Yup, that makes more sense. (Of course, there's the corner case of passing a struct value that contains a reference, etc...) – Jon Skeet Sep 03 '16 at 12:39
0

Your problem has nothing to do with a concurrent access. Your code is perfectly thread-safe.

I see two possible problems:

  1. You do not wait for the threads to finish. Your application abruptly aborts. The exception can be just a side effect of that abort.

    Use the Thread.Join to wait for the threads to finish at the end of the Multi_Thread method:

    a.Join();
    b.Join();
    
  2. The server may have problems with multiple parallel transfers due to a lack of available ports. Did you test parallel transfers from the same server using a standalone FTP client?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992