Active FTP Connections using Jscape in c#

FTP is a TCP based service exclusively. There is no UDP component to FTP. FTP is an unusual service in that it utilizes two ports, a ‘data’ port and a ‘command’ port (also known as the control port). Traditionally these are port 21 for the command port and port 20 for the data port.

Active FTP

In active mode FTP the client connects from a random unprivileged port (N > 1023) to the FTP server’s command port, port 21. Then, the client starts listening to port N+1 and sends the FTP command PORT N+1 to the FTP server. The server will then connect back to the client’s specified data port from its local data port, which is port 20.

Passive FTP

In order to resolve the issue of the server initiating the connection to the client a different method for FTP connections was developed. This was known as passive mode, or PASV, after the command used by the client to tell the server it is in passive mode.

In passive mode FTP the client initiates both connections to the server, solving the problem of firewalls filtering the incoming data port connection to the client from the server. When opening an FTP connection, the client opens two random unprivileged ports locally (N > 1023 and N+1). The first port contacts the server on port 21, but instead of then issuing a PORT command and allowing the server to connect back to its data port, the client will issue the PASV command. The result of this is that the server then opens a random unprivileged port (P > 1023) and sends P back to the client in response to the PASV command. The client then initiates the connection from port N+1 to port P on the server to transfer data.

Jscape is one of best 3rd party components used to develop ftp functionalities in your apps. The FTP class in Jscape provided the functionalities to create Active FTP connections.

The main properties in FTP class to use..

1. IsPassive

2. DataPortStart

3. DataPortEnd

4. PortAddress

The DataPortStart and DataPortEnd is the dataport range that are open in the server’s firewall exceptions. In case if the server exists in the Microsoft Azure VM, the data port range must be set in the Azure portal–> endpoint configurations.

PortAddress is the IP Address of the client from which the data connection is being initiated. In case we are trying to make an Active FTP connections from the Azure VM, by default it takes the static IP address of the VM until unless its specified explicitly.

Sample code looks like..

// Make connection…
Ftp ftp = null;
ftp = new Ftp(host, username, password);
ftp.LicenseKey = Constants.JsapeFTPLicenseKey;
ftp.Passive = !active;
if (active)
ftp.PortAddress = portAddress;
ftp.DataPortStart = Convert.ToInt32(dataPortStart);
ftp.DataPortEnd = Convert.ToInt32(dataPortEnd);

if (overwrite)
ftp.Overwrite = true;
if (remotedir != null)
ftp.RemoteDir = remotedir;
if (useBinary)
ftp.TransferMode = TransferModes.Binary;
ftp.TransferMode = TransferModes.Ascii;
catch (Exception ex)
throw new Exception(“FTP Connection Error: ” + ex.Message);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s