The idea
Moving a file is a different job from telling a server what to move. Commands are short lines of text. File contents can be text, binary or anything else. FTP does not mix these on one stream. It keeps a thin, long-lived channel for conversation and opens a separate channel only when a file has to cross.
How it works
Components
FTP is the standard protocol for transferring files from one host to another. The client has three components: the user interface, the client control process and the data transfer process. The server has two: the server control process and the server data transfer process.
Between client and server there are two connections, a control connection and a data connection.
How it works
Separate control and data connections
Separating commands from data transfer makes FTP more efficient. The two connections differ as follows.
- Control connection: TCP port
21. It uses very simple rules of communication, because it needs to transfer only a line of command or a line of response at a time. It remains connected during the entire FTP session. - Data connection: TCP port
20. It needs more complex rules because of the variety of data types transferred. It is opened and then closed for each file transfer activity.
| Control connection | Data connection | |
|---|---|---|
| Port | 21 | 20 |
| Carries | One line of command or response at a time | File contents, in a variety of data types |
| Rules | Very simple | More complex |
| Lifetime | Entire FTP session | Opened and closed for each file transfer |
How it works
Commands and responses
Commands are sent as ASCII text over the control channel. Responses carry a status code and phrase, as in HTTP. The lecture lists these samples.
Commands:
USER UserIDsupplies the user.PASS passwordsupplies the password.LISTlists subdirectories or files.RETR filenameretrieves a file from the server.STOR filenamestores a file to the server.
Responses:
331 Username OK, password required125 data connection already open; transfer starting425 Can't open data connection452 Error writing file
How it works
A transfer, step by step
Putting the lecture’s pieces in order for a download:
- The client opens the control connection to port
21and logs in withUSERandPASS. The server answers331to the user ID. - The client sends
LISTorRETR filenameover the control connection. - A data connection on port
20carries the listing or file. The server signals125when the transfer starts. - The data connection closes when that file is done. The control connection stays open for the next command.
Aside
The lecture's transfer example is a screenshot
The slide titled “FTP File Transfer Example” shows a terminal session against
a public FTP server: connecting, listing the files and accessing one file.
It is an image, and none of its text extracted, so the session output is not
reproduced here. The slide also notes that most operating systems ship an FTP
client for the terminal, reached with the ftp command, and that GUI clients
such as FileZilla exist.
Aside
Beyond the lecture
This note is outside the lecture and was not verified against the RFC text
for this page. The lecture glosses 452 as “Error writing file”. RFC 959
may word that code differently, so check it before quoting the RFC. The
exam marks the lecture’s wording. The lecture also describes only the
port 20 data connection and does not mention passive mode.
Where marks get lost
Do not say FTP uses port 21 for everything. Port 21 is the control
connection only. File contents move on port 20, and that connection is
opened and closed per file while the control connection persists.
In the exam
- Components: client has three (user interface, control process, data transfer process), server has two (control process, data transfer process).
- Ports: control
21, data20. Control lasts the whole session. Data lasts one file transfer. - Why separate: efficiency. Simple rules for commands, complex rules for the many data types.
- Commands
USER,PASS,LIST,RETR,STOR, all ASCII over the control channel. Responses are a code and a phrase.
Check yourself
- FTP uses two TCP connections: control on port
21, data on port20. - The control connection lasts the session and carries one line at a time. The data connection is opened and closed for every file.
RETRdownloads,STORuploads,LISTlists, andUSERandPASSlog in.- Responses look like HTTP: a status code and a phrase.