Question

We have a FTP Active client class we are using but we are trying to reuse the same data socket when transferring more than 1 file. To understand the behavior, I just used the ftp.exe (Active) client from Microsoft and then I uploaded 2 files as you can see below.

When using (Active) FTP client, the client issues a command PORT such as 192,168,150,80,14,178 (port 3762) for instance. Then the server on port 20 connects back to the client on its local port 3762 and the file is uploaded.

The problem is when I send the second file, the client issues another PORT command and it uses another local port such as 3763 for instance. This is the behavior of the Microsoft FTP client though.

However in our FTP class we have more control since we are doing all the bindings on all ports. I did manage to transfer the second 2 file using the same client local port but at some point the server sends info to client to close that port and the second file transfer then fails.

Is there a way to go around that behavior? we cannot use PASSIVE ftp at this point.

Thanks

C:\>ftp
ftp> open 10.10.10.10
Connected to 10.10.10.10.
220 Microsoft FTP Service
User (10.10.10.10:(none)): USER01
331 Password required for USER01.
Password:
230 User logged in.
ftp> cd test
250 CWD command successful.
ftp> put c:\temp\test1.docx
c:\temp\test1.docx: File not found
ftp> put c:\temp\test1.docx
200 PORT command successful.
125 Data connection already open; Transfer starting.
226 Transfer complete.
ftp: 13097 bytes sent in 0.42Seconds 31.11Kbytes/sec.
ftp> put c:\temp\test2.docx
200 PORT command successful.
125 Data connection already open; Transfer starting.
226 Transfer complete.
ftp: 13095 bytes sent in 0.61Seconds 21.54Kbytes/sec.
ftp> quit
221 Goodbye.

Basically in C# this is what we do:

System.Net.IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(sIPAddr), 4900); listening_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listening_sock.Bind(localEP); ... listening_sock.Listen(1); ... //send PORT command //send STOR command listening_sock.Accept(); ... //transfer file (via stream); ... //then try to issue another STOR command to transfer the file //if the port is still open it will transfer some data but eventually the ports closes itself.

Was it helpful?

Solution

In active mode the server side of the data connection is on port 20. If the client reuses the same IP and Port on its side you have the same connection tuple (e.g. client_ip,client_port,server_ip,server_port) for consecutive connections. But, with TCP you have to wait some time before you can reuse the same tuple. This means, that in active mode the client needs to announce a new port for each new data transfer.

See also the RFC959 (FTP) section 3.3 "Data Connection Management".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top