How FTP works

Control and data connections

Explain why an FTP session keeps commands separate from directory listings and file contents.

FTP uses a long-lived control connection for commands and replies. It normally reaches the server on TCP port 21.

Directory listings and file contents travel over separate data connections. A new data connection is normally created for each listing or transfer, then closed.

This separation lets commands remain responsive while data moves, but it means one apparent operation involves more than one TCP connection. Most FTP troubleshooting begins by asking which connection failed.

One listing uses both channels:

sequenceDiagram
  accTitle: FTP control and data connections
  accDescr: The client requests passive mode on the control connection, opens a separate data connection for the listing, reads its bytes, then waits for the final completion reply on the control connection.
  participant Client
  participant Control as Server port 21
  participant Data as Server port 50021

  Client->>Control: EPSV
  Control-->>Client: 229 passive port 50021
  Client->>Data: Open data connection
  Client->>Control: MLSD
  Control-->>Client: 150 opening data connection
  Data-->>Client: Directory facts, then EOF
  Control-->>Client: 226 transfer complete

The data connection has no FTP commands. Its bytes mean whatever the control command selected: a listing for MLSD, downloaded content for RETR, or uploaded content for STOR.

Closing the data socket is not final success. The client must still read 226 or another completion reply on the control connection. A server can consume all bytes and then report a filesystem error.

Draw the two TCP connections for this exchange. Label who initiates each connection, which reply is preliminary, and which reply proves completion.

Lesson completed

Take this course offline

Get every free book, course edition, and software download.

Get the download library →