title: Error : An existing connection was forcibly closed by the remote host in FTP In C#?
body: <p>When I am trying to do multiple upload i am getting the above error
  In FTP i have two classes one is for FTPdatatransfer and other is FTPcommand
  And in my main mage i am doing something like this</p>
  
  <pre><code>FTPCommand ftpcommand = new FTPCommand(ServerAddress, UserName, Password);
  ftpcommand.Login();
  ftpcommand.sendCommand("CWD ", "/home/praveena/RMS");  
  ftpcommand.sendCommand("PASV");              
  
  ftpcommand.sendCommand("STOR ","FTP.pdf");                 
  ftpcommand.sendCommand("STOR ","linux.pdf");
  ftpcommand.sendCommand("QUIT");
  </code></pre>
  
  <p>One upload is working fine while other gives above error</p>
  
  <p>FTPCommand.cs</p>
  
  <pre><code>public void sendCommand(String command, params string[] strfilename)
      {
  
  
          string ServerAddress = "172.24.18.240";
          string UserName = "praveena";
          string Password = "praveena";
  
  
          if (command == "STOR ") //Uploading to Server
          {
  
              FTPDataTransfer ftpdatatransfer = new FTPDataTransfer(ServerAddress, UserName, Password, 10000, iport);               
              foreach (string dir in strfilename)
              {
                  command = "STOR " + strfilename[0];
              }
  
              Send(command);                
              ftpdatatransfer.Upload(strfilename[0]);                
              this.readResponse();
  
          }
          else if (command == "MKD ")
          {
              command = "MKD " + strfilename[0];
              Send(command);
              this.readResponse();
          }
  
          else if (command == "CWD ")
          {
              command = "CWD " + strfilename[0];
              Send(command);
              this.readResponse();
          }
  
          else if (command == "NLST ") //Listing Files from Server.
          {
              FTPDataTransfer ftpdatatransfer = new FTPDataTransfer(ServerAddress, UserName, Password, 10000, iport);
              foreach (string dir in strfilename)
              {
                  command = "NLST " + "*";
              }
              Send(command);
              ftpdatatransfer.GetFilelist();
              this.readResponse();
  
          }
          else
          {
  
              Send(command);
              this.readResponse();
              if (command == "PASV")
              {
                  iport = GetPort();
              }
  
  
          }
  
  
      }
  
  
  
      public void Send(string command)
      {
          Byte[] cmdBytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());
          clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
      }
  
  
      public int GetPort()
      {
          int index1 = result.IndexOf('(');
          int index2 = result.IndexOf(')');
  
          string ipData = this.result.Substring(index1 + 1, index2 - index1 - 1);
  
          int[] parts = new int[6];
  
          int len = ipData.Length;
          int partCount = 0;
          string buf = "";
  
          for (int i = 0; i &lt; len &amp;&amp; partCount &lt;= 6; i++)
          {
              char ch = char.Parse(ipData.Substring(i, 1));
  
              if (char.IsDigit(ch))
                  buf += ch;
  
              else if (ch != ',')
                  throw new WinFTPClient.FTPCommand.FtpException("Malformed PASV result: " + result);
  
              if (ch == ',' || i + 1 == len)
              {
                  try
                  {
                      parts[partCount++] = int.Parse(buf);
                      buf = "";
                  }
                  catch (Exception ex)
                  {
                      throw new WinFTPClient.FTPCommand.FtpException("Malformed PASV result (not supported?): " + this.result, ex);
                  }
              }
          }
  
  
          string ipAddress = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3];
  
          int port = (parts[4] &lt;&lt; 8) + parts[5];          
  
          return port;
      }
  </code></pre>
  
  <p>FTPdatatransfer.cs</p>
  
  <pre><code>public void Upload(string strfilename)
      {
  
  
          Thread thread = new Thread(new ThreadStart(() =&gt; UploadFile(strfilename)));
          thread.Start();
  
  
  
  
      }
  
      public void UploadFile(string strfilename)
      {
  
  
          Socket cSocket = createDataSocket();
  
           //open stream to read file
  
          FileStream input = new FileStream(strfilename, FileMode.Open);
          while ((bytes = input.Read(buffer, 0, buffer.Length)) &gt; 0)
          {
              cSocket.Send(buffer, bytes, 0);
          }
  
          input.Close();
          if (cSocket.Connected)
          {
              cSocket.Close();
          }
  
         // this.readResponse();
  
          MessageBox.Show("File Uploaded successfully");
  
      }
  
   public Socket createDataSocket()
      {
  
  
          IPAddress ipAddress = IPAddress.Parse("172.24.18.240");  
          Socket socket = null;
          IPEndPoint ep = null;
  
  
          try
          {
              socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
              ep = new IPEndPoint(Dns.GetHostEntry(ipAddress).AddressList[0], port);
              socket.Connect(ep);
          }
          catch (Exception ex)
          {
              // doubtfull....
              if (socket != null &amp;&amp; socket.Connected) socket.Close();
  
              throw new WinFTPClient.FTPCommand.FtpException("Can't connect to remote server", ex);
          }
  
          return socket;
      }
  </code></pre>