Why does FTP use a control connection on port 21 plus a separate data connection for every file transfer or directory listing?
answer
- two connections, two jobs
- commands and replies stay on 21
- a listing is data too
- stream mode: close means end
- 150 before, 226 after
basics
~20 sFTP keeps commands and replies on a control connection to TCP port 21 and moves file contents and listings over separate data connections. In stream mode, closing the data connection marks end of file, so every transfer needs a new one.
solid answer
~50 sFTP (RFC 959) splits a session in two. The client opens a **control connection** to TCP 21 and keeps it for the whole session: `USER`, `RETR`, `LIST` and every numbered reply travel there as lines of text. Anything that is *content* — a file, or the output of `LIST` — goes over a **data connection** opened for that one transfer, bracketed by `150 File status okay; about to open data connection` and `226 Closing data connection`. One connection per transfer follows from the default **stream mode**: it has no end-of-file marker, so the sender signals end of file by closing the data connection, and a closed connection cannot carry the next file. Who opens each data connection — the server in active mode, the client in passive mode — is the other half of the design.
go deeper
Recall the split: commands and replies on port 21, content on a separate data connection for each file or listing, with a 150 reply typically before a transfer and 226 after it.
Explain why stream mode forces a new data connection per transfer: closing the connection is the end-of-file marker, so a closed connection cannot carry the next file.
Use the split when diagnosing: a session that logs in but fails at its first LIST has a healthy control connection and a data connection that could not be opened.
Weigh the two-connection design as a standing cost: every firewall, NAT and TLS layer in the path must handle a second, dynamically chosen connection that single-connection protocols avoid.
## Two connections, two jobs The File Transfer Protocol, defined in **RFC 959** (1985), never mixes commands and file contents on one TCP connection. A session uses two kinds: - a **control connection** — the client connects to the server's TCP port **21**, and that connection stays open from the greeting until `QUIT`. Commands go up it as lines of text; three-digit replies come back down it. - one **data connection** per transfer — a separate TCP connection that carries the bytes of one download, one upload or one directory listing, and is then closed. RFC 959 names the two halves the *protocol interpreter* (PI), which talks on the control connection, and the *data transfer process* (DTP), which moves bytes on the data connection. The split keeps the control channel responsive — a client can still send `ABOR` mid-transfer — and keeps binary content out of the reply grammar. ## What travels where | | Control connection | Data connection | |---|---|---| | Server port | **21** | **20** as the server's source port in active mode; a port the server picked in passive mode | | Lifetime | the whole session | one transfer or one listing | | Carries | commands and numbered replies, as text lines | file bytes, or the text of a listing | | Opened by | always the client | the server in active mode, the client in passive mode | State-only commands — logging in, changing directory — never touch a data connection. Commands that move content do: `RETR` (download), `STOR` (upload), and the listing commands `LIST` and `NLST`. ## Why a new connection for every transfer FTP defines three transfer modes — stream, block and compressed — and the default is **stream mode**. In stream mode with ordinary file structure the data is just bytes with no framing, so RFC 959 needs some other way to say "this file is finished": **end of file is indicated by the sending host closing the data connection**. That rule has a direct consequence: 1. The client asks for a file with `RETR`. 2. The data connection opens and the bytes flow. 3. The sender closes the data connection — that close *is* the end-of-file marker. 4. The server confirms on the control connection with `226 Closing data connection`. 5. The next `RETR` needs a brand-new data connection, because the old one no longer exists. For an upload with `STOR` the client is the sender, so the client's close marks the end. Block and compressed modes carry an explicit end-of-file marker, and RFC 959 lets a server keep a data connection open after such a transfer and confirm with `250 Requested file action okay, completed` instead of `226` — but stream mode is what clients and servers use in practice. RFC 1579 adds that clients pick a fresh local port for each data connection, so it does not collide with the previous one still in TCP's TIME-WAIT state. ## A directory listing is a transfer too Newcomers often expect `LIST` output to come back on the control connection like any other reply. It does not: a listing is content, so it travels on a data connection and is wrapped in the same replies as a file. A typical exchange in passive mode: | # | Direction | Line or action | |---|---|---| | 1 | C → S (port 21) | `PASV` | | 2 | S → C | `227 Entering Passive Mode (198,51,100,20,195,149)` | | 3 | C → S (data) | client opens a TCP connection to 198.51.100.20, port 50069 | | 4 | C → S (port 21) | `LIST` | | 5 | S → C | `150 File status okay; about to open data connection` | | 6 | S → C (data) | the listing text, then the server closes the data connection | | 7 | S → C | `226 Closing data connection` | Step 6 is end of file by close: the listing ends because the connection ends. A second `LIST` repeats steps 1 to 7 with a new port. ## Who opens the data connection RFC 959 lets either side open it, and that choice is the whole difference between the two modes: - **Active mode** — the client listens, tells the server where with `PORT`, and the server connects to it *from its port 20* (RFC 959's "L-1", one below the control port L = 21). - **Passive mode** — the client sends `PASV`, the server listens and returns its address and port in the `227` reply, and the client connects. If the data connection cannot be opened, the transfer command is answered with `425 Can't open data connection` on the control connection. The control connection itself is fine, which is why a session can log in happily and then fail on its very first listing. ## Misunderstandings worth correcting - Port 20 is not "the data port" in every case: it is the server's source port in active mode only. - The control connection does not close between files; only data connections do. - A listing is not a control-channel reply; it is a data transfer with `150` before it and `226` after it. - The second, dynamically chosen connection is why FTP needs special handling from firewalls and NAT devices that single-connection protocols avoid; what those devices rewrite is the subject of the material on FTP's NAT and firewall issues.
- Does an FTP server send the output of LIST over the control connection on port 21?No. A listing is content, so it travels on a data connection exactly like a file download. The control connection carries only the `150` before it and the `226` after it; the listing text arrives on the data connection, which the server closes to mark the end.
- Could FTP reuse one data connection for several files?Only outside stream mode. Block and compressed modes carry an explicit end-of-file marker, so RFC 959 lets the server leave the data connection open and confirm with `250` instead of `226`. In the default stream mode the close itself is the end-of-file marker, so every file needs a new connection.
- Why can an FTP client log in successfully and then fail on its first directory listing?Login uses only the control connection to port 21, which the client opened outward. The listing is the first command that needs a data connection — a second TCP connection in whichever direction the mode dictates. If it cannot be opened, the server answers `LIST` with `425 Can't open data connection`, or the client times out.
- FTP directory listings come back on the control connection like other replies
- Port 20 carries the data in both active and passive mode
- In FTP stream mode one data connection carries every file of the session
- The control connection closes after each file transfer
- FTP runs commands and file data over a single connection on port 21