File Transfer Protocol, for short
FTP, is an international standard protocol used for transferring
files between computers on Internet or Local Area Network (LAN).
FTP is based on TCP, so it is platform independent, computers
runing Windows can download files from those runing Unix, or computers
runing Mac OS can upload files to those runing Windows, etc.
If you want to share files on your computer for friends or
other people, you should run an FTP server program, then those
runing FTP client programs can download/upload files from/to your
Like other TCP protocols, FTP is a talk rule between client
and server. You are supposed to understand the basic concepts
of TCP/IP protocols, such as IP address, port, connection,
request, reply, etc.
Before explaining concepts of FTP, let's see a typical FTP
client sends command to server.
C<-S: means server replies to client.
C<-S: 220 server ready. C->S: USER anonymous
C<-S: 331 please send your e-mail as password. C->S: PASS email@example.com
C<-S: 230 user anonymous logged in. C->S: PWD
C<-S: 257 "/" is current directory. C->S: TYPE A
C<-S: 200 Type set to A. C->S: PASV
C<-S: 227 Entering Passive Mode (192,168,0,1,160,41). C->S: LIST
C<-S: 150 Opening data connection for "/".
C<-S: 226 Transfer complete. C->S: REST 0
C<-S: 350 Restarting at 0. C->S: TYPE I
C<-S: 200 Type set to I. C->S: PASV
C<-S: 227 Entering Passive Mode (192,168,0,1,160,42). C->S: RETR /20030501.log
C<-S: 150 Opening data connection for "/20030501.log"
C<-S: 226 Transfer complete. C->S: QUIT
C<-S: 221 bye-bye.
At the beginning of the conversation, server sent a welcome
message to client, and then waited commands from client, after
processing, replied to client. At last, client sent a "QUIT"
command, server replied, the connection was closed.
This conversation example is short, but it shows many important
characteristic of FTP.
1 Command. Client's each command is a text string line, starts
with command name, if it has parameter(s), the name is followed
by a space character and parameter(s) string. All commands
end with a <CRLF>.
2 Resoponse. Server's each response is a text string, starts
with a three-digit number, followed by a space character and
a human readable description string, and then ends with a
<CRLF>. The number is machine readable processing result
of client's last command, different number means different
result, success or failure, an FTP client program can understand
it. If the description string is multi-line, each line of
the response must begin with the result number, followed by
a '-', and then the description, except the last line. For
example: C<-S: 220- Hello, user anonymous,
C<-S: 220- welcome to this FTP Server!
C<-S: 220 server ready.
3 Control Connection and Data Connection. The TCP connection
between client and server is only used to send/receive commands/responses,
we call it control connection. If they want to transfer a
file or directory listing result, they must establish another
connection, transfer it, and then destroy the connection.
We call this data connection. The most important difference
between FTP and other TCP protocols (such as HTTP, TELNET,
SMTP, etc) is that an FTP conversation may have two TCP connections
at the same time: control connection and data connection.
4 Active Mode and Passive Mode. For transferring a file,
client and server must establish a data connection. Default
case is that server connect to client's port 20, but client
may change this by sending command 'PORT' or 'PASV'. Command
'PORT' tells server what address it should connect to. In
following example, server should connect to 172.18.0.2:2560
when need to establish data connection: C->S: PORT 172,18,0,2,10,0
C<-S: 200 PORT command success.
Both default case and command 'PORT' make server connect to
client. We call this active mode.
Sometimes active mode isn't enough, for example, client is
on Intranet and has a private IP, but server is on Internet.
In this case server can't connect to client, so we have command
'PASV'. Command 'PASV' asks for server to enter passive mode.
Server's response should contains the address that client
connect to. In following example, client should connect to
22.214.171.124:2560 when need to establish data connection:
C->S: PASV 172,18,0,2,10,0 C<-S: 227 Entering Passive Mode
Now you have understanded the most important four concepts
of FTP, you can use FTP server much more freely!