How the File Transfer Protocol works
Basic concepts
The File Transfer Protocol (FTP) specification was created to transfer files reliably and efficiently between incompatible computer systems. An FTP conversation consists of commands sent by the client software (eg. FTPShell Client) and responses sent by the server software (eg. FTPShell server).
After the client connects, the server sends a response to the client, either accepting or rejecting the connection. This initial response is called the greeting. If the server accepts the connection, the client sends commands to the server. The server sends one or more responses to the client.
Commands and Responses
An FTP command is a string of bytes. It contains a verb consisting of alphabetic ASCII characters, a space followed optionally by a parameter and a terminating character sequence(\r\n).
The server's response consists of one or more lines. Each line is terminated by a terminating character(\r\n). The last line of the response contains a three digit ASCII code. Codes between 100 and 199 indicate marks, codes between 200 and 399 indicate acceptance while codes between 400 and 599 indicate rejection.
Usernames, Passwords, and Files
An FTP server provides access to a collection of files. Each file is identified by a server-defined username and a server-defined pathname. Many servers provide public files under the standard username anonymous. Most of these servers demand a password, but allow any password that ends with @. FTP defines three types of files - text files, binary files, and directories.
The client normally begins each FTP connection with a USER request, then, depending on the servers response, a PASS request. A USER request has a parameter showing a username. A PASS request has a parameter called a password.
Mechanics of file transfer
An FTP session consists of two separate network connections known as the control connection and the data connection. Both the control and data connections use TCP. The use of separate connections enables selection of different qualities of service e.g. minimum delay for the control connection and maximum throughput for the data connection.
Data transfer is always initiated by the client though either may supply the data (download vs upload). The data transfer mechanism is also used to transfer directory listings. A RETR request asks the server to send the contents of a file over the data connection. The RETR parameter is the pathname of the file. The file is transferred either in binary or text mode, depending on the most recent TYPE request.
To resume downloads, the REST request is used with a file size parameter. If the file size parameter is n, the server will omit the first n bytes of the data that it sends through the data connection. To upload files, a STOR request is issued. A STOR request asks the server to read the contents of a file from the data connection. The STOR parameter is the pathname of the file. To resume uploads the APPE command is used. APPE is similar to STOR except that if the file already exists, the server appends the client's data to the file.
The Passive Mode
In a normal connection, the data connection is established by the server. However, if the client is accessing the server through a client-side firewall, the security features of the firewall will not allow the remote server to connect to a data port. Hence the passive mode (PASV request) needs to be used. In a PASV connection, the data connection is established by the client. The server responds back to the client with a port number that it has allocated. The client then initiates the data connection to the IP address and port number provided by the FTP server.
Furthur reading
Implementation details of the FTP protocol including various reference documents are available here. A comprehensive list of all FTP commands and their descriptions is available here. Information on a simple FTP scripting language is available here
Questions people ask
What does FTP stand for?
File Transfer Protocol: the standard, defined in RFC 959 in 1985, for moving files between a client and a server over TCP/IP.
Explain FTP: what is FTP in computer networks, and the FTP client definition
In computer networks FTP is the protocol a client program uses to log in to a server, list its folders and copy files in either direction, using one connection for commands and a second for each transfer. An FTP client is the program on your side of that conversation; an FTP server is the program on the other side that holds the files and checks your login.
What is FTP used for, what is FTP file sharing, and what are FTP file sharing sites and FTP sharing on Windows 10?
FTP is used for publishing websites, moving backups and logs, distributing software and exchanging files between organisations. FTP file sharing means giving other people a login, or anonymous access, to a server so they can fetch or deposit files; FTP file sharing sites are public servers that distribute software and data that way. To share files from a Windows 10 PC over FTP you run an FTP server on it, such as FTPShell Server, and others connect with a client.
Does FTP use TCP or UDP, and which layer is FTP: the application layer?
FTP uses TCP, not UDP, for both its control and data connections. In the TCP/IP model it sits at the application layer, alongside HTTP and SMTP, above the transport layer that TCP provides.
What are the FTP transfer types: ASCII type A and binary mode?
FTP has two transfer types. ASCII, type A, converts line endings between systems and is only for plain text files. Binary, type I, sends the bytes exactly as stored and is right for everything else: images, archives, programs, documents. Sending a binary file in ASCII mode corrupts it, which is why clients such as FTPShell default to binary and switch to text only for names on a text-pattern list.
What is an FTP URL?
An address of the form ftp://user:password@host:port/path, which names the server, optionally the login, port and starting folder. Paste one into FTPShell and a session opens on that folder; the login and password parts are optional and are better left out of anything you write down.
What is anonymous FTP?
A server account that anyone can use, with the login anonymous and an e-mail address as the password by convention, giving read access to public files. Software archives and standards bodies still distribute files this way; FTPShell's new sessions start with an anonymous login filled in for exactly that case.
FTP client-server architecture: how the two sides work
The client opens a control connection to the server's port 21 and sends commands; the server replies with numbered codes. For each listing or file, a separate data connection is opened, by the server in active mode or by the client in passive mode, and closed when the transfer ends. The server keeps the files and enforces logins and permissions; the client keeps the user interface, the queue and any automation.
