Jalal Mostafa
Jalal Mostafa

Reputation: 1024

FTP Active Mode and multiplexing

FTP RFC 959 specifies that the data connection is opened by the server from port 20 (default) to a random port in the client and known by the server through a PORT h1,h2,h3,h4,p1,p2 command. This is called Active Mode Transmission.

so that the host is h1.h2.h3.h4 while the port is p1 * 256 + p2.

My Question is: How can the server initialize multiple connections to multiple clients via the same port which is 20 by default?

Imagine client c1 has an established connection with server data port 20 and is transferring data, how can client c2 establish a connection with server if data port is already used by a TCP connection?

Upvotes: 1

Views: 1037

Answers (1)

mrahhal
mrahhal

Reputation: 3497

A server implementing Berkeley's sockets goes through a couple of phases when accepting connections. A lot of the plumbing is generally handled by the framework or the operating system, I'll try pointing them out. I'll try explaining this below with some pseudo-code.

1: Binding to the listening port

The server first asks the kernel to bind to a specific port to start listening on:

void* socket = bind(20);

2: Accepting a connection

This is probably the point that causes some misconceptions. The server gets a connection through the bound socket, but instead of using the listening port (20) to handle the communication with the new client it requests a new (random) port from the kernel to be used for a new socket connection. This is typically handled by the operating system.

void* clientSocket;

// Block until a client connects. When it does,
// use 'clientSocket' (a new socket) to handle the new client.
socket->accept(clientSocket);

// We'll use 'clientSocket' to communicate with the client.
clientSocket.send(someBuffer, ...);

// 'socket' is free again to accept more connections,
// so we can do it again:
void* clientSocket2;
socket->accept(clientSocket2);

// Of course, this is typically done in a loop that processes new connections all the time.

As a summary, what's happening is that the listener socket (20) is used only for accepting new connections. After a client establishes connection, a new socket is created to handle that specific connection.

You can test this by examining the socket connection you get as a client after establishing connection. You'll see that the remote port is not 20 anymore (it will be a random port chosen by the remote server).

All of this is shared by tcp, ftp and any protocol using the sockets protocol under its hood.

Upvotes: 1

Related Questions