0
votes

I am writing FTP client with C#. I typed methods to upload file to FTP server and file upload does work. However, after successful data transmission, client gets disconnected from server. Here are steps I do: 1. Get IP and Port from server by using PASV. 2. Create DATA connection with server using IP and port. 3. Convert file to bytes and send through DATA connection. 4. Send STOR through COMMAND connection

My question is why I get disconnected.

public void PrepareUpload() // Get IP and Port from server by using PASV.
        {
            String answer;
            String message = "PASV\r\n";
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
            this.ns.Write(data, 0, data.Length);      
            answer = Response(this.ns);
            this.dataPort = getPort(answer, 4) * 256 + getPort(answer, 5);
        }

public void DataConnect(string server) // Create DATA connection with server using IP and port.
        {
            int port = this.dataPort;
            this.dataConnection = new TcpClient();
            IPAddress ipAddress = Dns.GetHostEntry(server).AddressList[0];

            this.dataConnection.Connect(ipAddress, port);
            this.nds = dataConnection.GetStream();  
        }

public void DataTransfer(string filename) // Convert file to bytes and send through DATA connection.
        {
            byte[] data = System.IO.File.ReadAllBytes(filename);
            this.filename = Path.GetFileName(filename);
            nds.Write(data, 0, data.Length);
        }

public void Upload() // Send STOR through COMMAND connection
        {
            String message = "STOR " + this.filename + "\r\n";
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
            this.ns.Write(data, 0, data.Length);
        }
1
Do not post code as an image. Post it as text + Moreover your code does not show, how are the methods called + Explain, how do you detect the disconnection - What exactly happens? - Network capture or at least a server log would be useful too.Martin Prikryl
After successful upload, I try to use other functions I implemented: directory change, NLIST function, RETR function. I get no response from server side after successful file upload. If file upload fails or it is not even used other functions work well.user7038346
"No response" and "disconnect" are two different things. If you need our help, you need to be exact. And provide the information, I've asked you for.Martin Prikryl
Why not use the included facilities on C# to connect to a FTP server? Check FtpWebRequest.Gusman
It is not allowed to use these libraries for our assignment.user7038346

1 Answers

1
votes

The order you describe is wrong. In particular you should not start data transfer before issuing the command which specifies what should happen with the transferred data (i.e. STOR). The correct order would be:

  1. Use PASV or PORT command to determine data port and get response to this command. In case of PORT listen on the given IP:port.
  2. Send the STOR command and read the response. It should be a preliminary response (150).
  3. Create the data connection: with PASV connect to the remote host, with PORT wait for an incoming connections.
  4. Transfer the data and close the data connection.
  5. Wait for the final response (226).