ELEC3506

Topics

Application LayerLecture 8 PDF10 min

FTP

How FTP splits commands and file data onto two TCP connections, which ports each uses, and the sample commands and response codes the lecture lists.

By the end of this page you should be able to

  • Name the three client components and two server components of FTP
  • State which port carries the control connection and which carries the data connection
  • Explain why separating control from data makes FTP more efficient
  • Match the sample FTP commands and response codes to what they do

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 connectionData connection
Port2120
CarriesOne line of command or response at a timeFile contents, in a variety of data types
RulesVery simpleMore complex
LifetimeEntire FTP sessionOpened 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 UserID supplies the user.
  • PASS password supplies the password.
  • LIST lists subdirectories or files.
  • RETR filename retrieves a file from the server.
  • STOR filename stores a file to the server.

Responses:

  • 331 Username OK, password required
  • 125 data connection already open; transfer starting
  • 425 Can't open data connection
  • 452 Error writing file

How it works

A transfer, step by step

Putting the lecture’s pieces in order for a download:

  1. The client opens the control connection to port 21 and logs in with USER and PASS. The server answers 331 to the user ID.
  2. The client sends LIST or RETR filename over the control connection.
  3. A data connection on port 20 carries the listing or file. The server signals 125 when the transfer starts.
  4. 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, data 20. 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 port 20.
  • The control connection lasts the session and carries one line at a time. The data connection is opened and closed for every file.
  • RETR downloads, STOR uploads, LIST lists, and USER and PASS log in.
  • Responses look like HTTP: a status code and a phrase.

Check yourself

  1. Which port does the FTP control connection use, and how long does it stay open?
  2. Why does the data connection need more complex rules than the control connection?
  3. A user wants to download a file from the server. Which FTP command is used?
  4. The server replies "331 Username OK, password required". What does the client send next?
  5. How many components does the FTP client have, and how many does the server have?