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);
}