How FTP works

Control and data connections

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

8 minute lesson

~~~

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:

control: C: EPSV
control: S: 229 Entering Extended Passive Mode (|||50021|)
data:    client -> server:50021
control: C: MLSD
control: S: 150 Opening data connection
data:    directory facts, then EOF
control: S: 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 and course as PDF and EPUB files.

Get the download library →