FTP protocol fully detailed

Source: Internet
Author: User
Tags ftp commands response code terminates ftp protocol server port

1. Introduce

The goal of FTP is to improve file sharing, to provide indirect use of remote computers, and to make storage media transparent and reliable and efficient for users to transmit data. Although we can also use it manually, its main function is for the program to use. It is a good idea to read the TCP protocol standard specification and the Telnet protocol standard before reading this article.

2. Overview

In this section we will discuss some of the superficial issues, and some definitions of nouns refer to the TCP and Telnet references. Let's start with (1) byte size, with two bytes in ftp: Logical byte size and byte size for transmission. The latter is usually 8 digits, and the former may not be much. The transport byte does not have to be equal to the logical byte size, nor does it need to interpret the data structure. (2) A control connection is a communication link established between User-pit and SERVER-PI for exchanging commands and responses. (3) The data connection is a FULL-DUPLEX connection of the transmitted data. Transmission data can occur between the server DTP and the user DTP can also take place between two server DTP. (4) DTP: Data transmission Process (DTP) to establish and manage information connections, DTP can be active or passive. (5) Eor represents the end of the record. (6) NTV represents a network virtual terminal, which is defined in accordance with the definition in the Telnet protocol. (7) NVFS represents the network virtual file system. (8) FTP can transmit noncontiguous files, which are part of the file called pages. (9) Pi represents the protocol interpreter. (10) The server DTP represents a transport process that is typically in the "active" state, establishes a data connection with the listening port, and also sets parameters for transmission and storage, and transmits data based on PI instructions. Of course, DTP can also be transferred to the "passive" state. (11) Server FTP process, which is working with the user FTP process, which consists of pi and DTP. The user FTP process is made up of PI,DTP and user interfaces. The following figure is a schematic of the FTP service:

Note: The data connection is bidirectional, and it does not have to exist all the time. The user pi in the image above begins to control the connection and the control connection is much like the Telnet protocol. In the initial phase, the standard FTP command is generated by the user pi and transmitted to the server process through a control connection. The server PI returns the standard response to the user pi. The FTP command specifies data connection parameters and file system operations. The user DTP listens on a specific data port, the server starts the data connection, and begins the transfer with the specified parameters. The data port does not have to be on the machine that started the FTP command, but the user or user FTP process must determine that it listens on the specified data port. This data connection is Full-duplex.

In another case, the user might want to transfer files between two hosts, not two local hosts. The user establishes a control connection between the two hosts and then plans the data connection. In this way, the control information is obtained by the user pi, but the data is transmitted between the server DTP. Here's an example:

Protocol requires data transfer to open a control connection during processing. The user aborts the control connection after completing the FTP service, and the server is specific. If the control connection is turned off when the command is not received, the data transfer is also turned off by the server. FTP and Telnet are very connected, FTP using the Telnet protocol to control the connection, there are two ways to achieve the goal: User pi or server pi can be implemented in their own process Telnet protocol function; The second method is to take advantage of the existing Telnet module in the system. Implementation, FTP on the Telnet protocol is not much, even if the implementation, the amount of code is not large.

3. Data transmission function

The data connection transmits only data, controlling the connection Transfer command and response. Several commands are about transferring data between hosts, and data transfers are essentially independent of the physical structure, but the properties of the file are related to the presentation type if the stream is related to the file structure in the compressed transport mode.

3.1. Data representation and preservation

Data is transferred between storage settings between hosts. Because the data is stored differently between the two systems, therefore, it needs to be converted, when the text will be transmitted to the problem of ASCII, in the binary transmission, there will be different systems for the length of the byte of different problems, some systems are 7 bits, and some systems may be 32 bits, this also needs to be converted. Need to provide data representation and transfer model functions, but FTP provides this aspect of the function is not much, more than FTP to provide the functionality of the user to achieve their own.

3.1.1. Data type

A data representation is a type of representation specified by the user, which can be implied or user-specified. Be sure to note that the logical byte length differs from the physical byte length. ASCII type

This is the default type that all FTP must implement to transfer text files that are more convenient when using EBCDIC transfer between hosts, and do not use the ASCII type. The sender converts the internal representation to the NVT-ASCII format and the receiver receives the data in the opposite process. According to the NVT standard, the <CRLF> sequence is used at the end of the row. The NVT-ASCII is 8-bit. The format parameters for ASCII and EBCDIC are discussed below. EBCDIC type

It is another method of ASCII as a data type for transferring data between hosts. EBCDIC and ASCII are very much alike, with only a few differences in the functional description of the type. Line terminator is used very little. Image Type

The data that is transferred under this type is considered a contiguous bit, and the sender packs the data into a 8-bit transport byte. Because the structure needs to populate the transfer data, the padding bytes are all 0, the padding must be used in the file structure and marked out for the receiver to filter out. It is used to transfer binary data and efficiently transfer and store files, so all FTP must also be implemented. Local type

You can also specify the logical byte size in decimal. If the physical byte size differs from the logical byte size, the physical data is packaged directly into logical bytes without any padding. The receiver converts from the logical byte size to the storage characteristics of the native. The transmission must be repeatable, that is to say, the same file has the same parameters, and that content must be the same. Format Control

ASCII and EBCDIC have an optional parameter that describes the vertical format control of the file, and the following data representation type is defined in FTP. Character files can have three uses, printed, stored, or left to be processed later. If it is for printing, the host must know the representation of the vertical format control, and if it is stored or processed later, the file format needs to be preserved. If the remote host is processed and transferred back to the local host, there is no trouble keeping the remote host processing. This requires the addition of new parameters in both ASCII and EBCDIC formats. NON PRINT

The second argument is not specified as it is the default value. It must be accepted for all FTP. If the file being transferred is for printing, the default values for boundaries and intervals are used. Typically, it is not used for printing purposes, but for saving files or executing files. telnet format Control

Files include ascii/ebcdic vertical format control, which enables printing to work properly. Carriage Control (ASA)

file contains ASA (FORTRAN) vertical format control character. The first character is not printed in a row formed with ASA Standard, which determines the amount of paper to go before printing. The following is the ASA character defined:

Blank: Move down 1 lines, 0: Move Down 2 lines, 1: Move to next page; +: Do not move

The printer must be able to determine the end of the structure. If the file itself has a record structure, there is no problem, and if no,<crlf> is used to differentiate the print lines, these formatting marks are already used by the ASA control character.

3.1.2. Data structure

In addition to having different data types, FTP also allows for different file structures, below are three file structures: File structure: There is no internal structure in the file, the file is considered as binary stream; Record structure: A file is made up of a series of records; page structure: A file is made up of different index pages.

If the Stru command is not used, the file structure is the default value. The structure of the file affects the transport model, storage, and data representation. The properties of the file are related to the host that saved it, and different machines save the file in their own way. When transferring files between different hosts, the host must be able to recognize each other's representations. Some hosts have files that are byte-oriented, some are log-oriented, and problems occur when they are delivered. That's going to be an internal conversion at the receiving party. When converting, you need to distinguish between the bounds of records, use <CRLF> in ASCII, and use <NL> as delimiters in EBCDIC. The implementation of this method must ensure that the conversion is reversible. File structure

If the Stru command is not used, the file structure is the default value. There is no default value in the file structure, and the file is considered a contiguous byte string. Record structure

For text files, the record structure must be available for all FTP implementations. A record structure file is made up of successive records. Page structure

The page structure is used when the file is non contiguous. This file is called a random Access file. Sometimes the information in these files is related to the whole or part of the file. In FTP, a part of a file is called a page. To provide different page sizes and related information, each page is sent with a page of hair. The following fields are in the page header:

Head length

Include the length of the page header of this byte, in bytes, with a minimum length of 4

Page index

Indicates the location of this section in the original file and it is not the same as the transmission number

Data length

The number of logical bytes in the page data, with a minimum value of 0

Page type

There are several types of pages: 0 = Not paged, indicating the transport structure, the header must be 4, the data length must be 0;1= the usual page, the usual type of page file without control information; 2 = The description of the child page, which is used to transmit the description of the whole file; 3 = Access Control page, including page File header field for page level access control information, The length of the header must be 5

Optional domain

Provides control information for each page

Description 8 bits

byte Count 16 bits

The description of the child code is described by the bit tag in the description child byte, and the following are four codes that have been specified and their meaning:




Data Block End is EOR


Data Block End is EOF


There is a suspect error in the data block


Data block is a restart tag



With this encoding, there may be multiple descriptive child encoding conditions for a particular block, and the required bits must all be set. The restart tag is included in the data flow as a 8-bit integer representing the printable byte of the language used on the control connection, but <SP> must not appear in it. For example, to transfer a 6-byte tag, here's an example:

Descrptr Code = 16

Byte count = 6


Marker 8-bit

Marker 8-bit

Marker 8-bit


Marker 8-bit

Marker 8-bit

Marker 8-bit

3.4.3. Compression mode

There are three kinds of information that needs to be transmitted: regular data is sent in a byte string, compressed data, including replicas and filters, and control information transmitted in two escape characters. If there is an n (>0 but less than 127) regular data transfer, a byte at the top of the data, a byte with a maximum bit of 0, and the other bits representing the number equals N. The following figure:

To compress the n copy string of a data bit d, transfer it in two bytes, as shown in the following figure, which represents a compressed byte:

A string of N-Filter bytes can be compressed into one byte, while the filter byte varies according to the notation. If the type is ASCII or EBCDIC, the filter byte is <sp>, or 0 if it is an image or a local type. The following is the filter byte:

The escape sequence is a double byte, the first one is all 0, and the latter is a description code defined in block mode. The description code is the same as the meaning in block mode, and it acts on the bytes in the subsequent string. Compression mode is good for increasing bandwidth (because the data is compressed).

3.5. Error recovery and restart

Bit loss and data errors are the responsibility of this layer of error control by TCP. and restart is to give users a way to deal with the system (including operating system, network, etc.) failed. The restart process applies only to blocks and compression modes, and it requires the sender to include a specific tag in the data stream that is meaningful only to the sender, but the content is the language that controls the connection. A tag can represent a bit count, record a count, or any point that can mark the number of data. If the receiver also supports a restart, the token will be saved on the receiving system. After the system fails to reboot, the user can restart the data transfer according to the original token, which is our usual familiar breakpoint continuation process. The receiver receives a piece of data and then notes that if the sender fails, the corresponding data is transmitted from that point onwards. The concrete here is not much to say.

4. File Transfer function

The channel from the user pi to the server is established between the user and the tag server port. The user protocol interpreter is responsible for sending commands and interpreting replies received; Server Pi interprets commands, sends answers, directs DTP to establish data connections and transmits data. If the second party of the data transmission is the user DTP, it is controlled by the internal protocol of the user's FTP host; if the second party is a server DTP, it is controlled by the user Pi command via its own pi.

4.1. FTP command

4.1.1. Access Control command

The following commands specify the access control token (the command code is in parentheses):

Username (user)

parameter is a Telnet string that marks the user. User tags are required to access the server, which is usually the first command to be issued after the connection is controlled, and some hosts will also require passwords and accounts. The server can receive new user commands at any time to change access control and/or account information. This can restart the login process, so the transmission parameters are unchanged and the file transfer in progress is completed under the previous access control parameters.

Password (pass)

parameter is a Telnet string that marks the user's password. This command immediately follows the user command, and at some sites it is an integral step in completing access control. So the password is an important thing, so it cannot be displayed, the server side has no way to hide the password, so this task has to be completed by the user FTP process.

Account (ACCT)


parameter is a Telnet string that marks the user account. This command does not need to be related to user, some sites may require an account for logging on, others can restrict the permissions of the account, and in the latter case, this command can be sent at any time. Different responses can be different: When the login requires account information, the response to the Pass command is 332. Also, if you don't need account information, the response to pass is 230, and if you need the account information to be needed later, the server will return 332 or 532, whether it's saving the command or rejecting the command.

Change Working directory (CWD)

This command enables a user to work in a different directory or dataset without changing its logon or account information. The transfer parameters are unchanged. parameter is typically a directory name or a system-related collection of files.

Go back to the previous level directory (Cdup)

This command requires the system to implement the directory tree structure, which responds as CWD.

Structure loading (SMNT)

This command enables the user to load another file system data structure without changing the logon or account information. The transfer parameters are unchanged. The parameter is a file directory or a system-related collection of files.

Reinitialization (REIN)

This command terminates user, writes all I/O and account information, but does not allow data transfer to be done in. Reset all parameters, control the connection to open, and start the user command again.

Exit Login (QUIT)

This command terminates user and, if there is no data transfer, the server shuts down the control connection and, if there is data transfer, the server shuts down the control connection after receiving the transmission response. If a user process is transferring data to a different user, and you do not want to close and then turn on each user, you can use rein. Accidental shutdown of the control connection can cause the server to run abort (Abor) and exit login (QUIT).

4.1.2. Transfer Parameters Command

All data transfer parameters have default values. The server must record a default value that can be sent in any order after the FTP service is requested. The following command routes the parameters:

Data Ports (port)


parameter is the data connection port to use, which is typically not required for command response. If you use this command, you send a 32-bit IP address and a 16-bit TCP port number. The information above is transmitted in 8-bit order, comma-separated decimal, as the following example:

PORT H1,H2,H3,H4,P1,P2

Where H1 is the highest 8 bits of IP address.

Passive (PASV)

This command requires the server DTP to listen on the specified data port and enter the status of the passive receive request, which is the host and port address.

Representation type (type)

parameter to specify the presentation type. Some types require a second parameter, the first parameter is defined by a single Telnet character, and the second argument is a decimal integer that specifies the byte size, with <SP> delimited between the parameters. The following are the formats:

The default representation type is ASCII nonprinting characters, and if the parameter is unchanged and only the first argument is changed later, the default value is used.

File Structure (Stru)

parameter is a Telnet character code that specifies the file structure. Here's the code and its meaning:

F-File (not a record structure), it is the default value

R-Record structure

P-page structure

Transfer modes (mode)

parameter is a Telnet character code that specifies the transport mode. Here's the code and its meaning:

S-Stream (default value)



4.1.3. FTP Service Command

The FTP service command defines the file transfer or file system functionality requested by the user. The argument for this command is usually the pathname, and its syntax is consistent with the server's specification. The recommended default value is the recently specified device directory or directory. Command order is usually unrestricted, only the "rename from" command must be followed by "Rename to", and the restart command must be followed by an Interrupt service command. The response of a service command is usually transmitted over a data connection. The following are the specific commands:

Get File (RETR)

This command enables the server DTP to transfer a copy of the file within the specified path to the server or user DTP. The status and content of files on this side of the server are unaffected.

Save (STOR)

This command causes the server DTP to receive data from the data connection and to store the data in the server's file. If the file already exists, the original file will be overwritten. If the file does not exist, create a new file.

Unique Save (Stou)

This command is similar to Stor, which requires that the file name in this directory be unique, and that the response to this command must include the resulting user name.

Additional (AppE)

It is similar to the Stor function, but if the file already exists within the specified path, append the data to the end of the original file, or create a new file if it does not exist.

Allocation (ALLO)

This command is used to allocate enough storage space on some hosts for newly transferred files. parameter is the number of logical bytes in decimal. If it is a record or page structure, the maximum size of the page or record is also required, which is specified in decimal in the second argument. The second argument is optional, and if it is, it is separated from the first parameter in Telnet character <SP> R <SP>. This command, after the Stor or AppE command, is equal to NoOp for machines that do not need to allocate storage space.

Start Again (REST)

The parameter field represents the point at which the server wants to restart, not the file, but the data after the specified point, which should be followed by an FTP command that requires file transfer.

Renaming (RNFR)

This command is the same as what we use in other operating systems, except that we will specify a new filename with "Rename to" later.

Rename to (Rnto)

This command, together with the above command, completes the renaming of the file.

Give Up (Abor)

This command notifies the service to abort the previous FTP command and the data transfer associated with it. If the previous operation is completed, there is no action and returns 226. If it is not done, return 426 and then return 226. Close the control connection and the data connection does not close.

Delete (DELE)

This command deletes the file under the specified path. The user process is responsible for prompting for the deletion.

Delete directory (RMD)

This command deletes the directory.

Create a table of contents (MKD)

This command creates a new directory under the specified path.

Print working directory (PWD)

In the response is to return to the current working directory.

Lists (list)

Server routing list to passive DTP, if the path specifies a directory or many files, returns a list of files under the specified path. If the path name specifies a file, the server returns the current information for the file, and the parameter is null to represent the user's current working directory or default directory. Data transfers are performed in ASCII or EBCDIC, and users must confirm this. Because file information varies depending on the system, it is not possible for the program to be used automatically, but the human user needs it.

List of names (NLST)

The Server transfers directory table name to the user, the pathname should specify the directory or other system-specified file group descriptor, and the null parameter refers to the current directory. The server returns the file name data stream, which is transmitted in ASCII or EBCDIC form and separated by <CRLF> or <NL>. The information returned here can sometimes be further processed by the program.

Web parameters (site)

The server is used to provide server system information, the information varies depending on the system, and the format is given in the Help site command response.

System (Syst)

Used to determine which operating system is running on the server.

Status (STAT)

This command returns the control connection state, which can be sent during file transfer, and the server returns the status of the operation. It can also be sent between file transfers, when the command has parameters, and the parameter is the pathname, and the functionality of the command is similar to the list command except that the data is transferred over the control connection. If you specify a partial path, the server returns with a filename or a description-related property, and if there are no parameters, the server returns the status information for the server FTP process, including the current value and connection state of the transport parameter.

Assistance (help)

This command is no different from the help we get in the normal system, and the response type is 211 or 214. It is recommended that you use this command before using the user command.

Waiting (NOOP)

This command does not produce any actual action, it only causes the server to return OK.

FTP uses Telnet to communicate on a control connection, so there is an opportunity to see the relevant protocol instructions. It would be good to understand the following. The following sections describe the response to the command and details about the command. FTP commands can be divided into access control tags, data transmission parameters or FTP service requests, and specific commands (such as ABOR,STAT) can be transmitted over the control connection during data transfer. Some servers cannot monitor data and control links at the same time, so there is a need to take additional steps. Please note the following suggestions:

1. The user system inserts Telnet "Interrupt Process" (IP) information into the Telnet stream;

2. The user system sends Telnet "Synch" signal;

3. The user system inserts commands (such as Abor) into the Telnet stream;

4. server pi after receiving IP, in the Telnet stream to find only one of the FTP command.

4.2. FTP Answer

The FTP command responds in order to synchronize data transfer requests and procedures, and also to let users understand the state of the server. Each command must have at least one response, and if it is more than one, they are easy to distinguish. Some commands are sequential, so failure of any of these commands can lead to a start from scratch. The FTP response is composed of three digits followed by some text. Numbers with enough information to name the user pi don't check the text to know what's going on. Text information is related to the server, and users may get different textual information. Text and numbers are at <SP> intervals, and the text ends with a telnet line terminator. There may be more than one line of text, in which case the text must be in parentheses, and the first line of information indicates that the text is more than a row and the last line is marked. If it's more than one line, you can add "-" to the number code, the last line starts with a number, followed by <sp>, plus the line terminator at Telnet. Here is an example:

123-The first line

Second line

234 a line starting with a number

123 Last line

Three digits each have a certain meaning, the first to determine whether the response is good, bad or incomplete, by examining the first, the user process is usually able to know what to do roughly. If the user program wants to know what the problem is, you can check the second one and leave a third to indicate additional information. The first digit has five values:

1yz Confirm Preliminary response

The requested operation is being initialized, and waits for another response before entering the next command. This type of response is used to illustrate that commands are accepted, and how to synchronize monitoring in the implementation is difficult, and the user process can now focus on data connections. The server FTP process almost returns a 1YZ response to the first command.

2yz Confirm Completion Answer

The requested operation is complete and you can execute the new command.

3yz Determine intermediate response

The command was accepted, but the requested operation was stopped, and the updated information was stopped.

4yz temporarily refuses to complete the answer

The command was not accepted and the requested operation was not executed, but the error is temporary and can be sent again in a few minutes. The user should return the start of the command sequence. This is not OK for the moment, this command means that the user process tries to use this command again.

5yz always refuses to complete the answer

The difference between it and a temporary refusal to complete a reply is that the error condition is that 1:30 will not disappear.

Let's take a look at what the second place means:

x0z format error;

x1z this kind of reply is to request the information;

X2Z This kind of response is about control and data connection;

x3z about authentication and account login process;

x4z not used;

x5z This type of response is about the file system;

The third number is a further refinement of the answer on the basis of the second digit. In general, digital code should have text information. Implementation should use existing code as much as possible, rather than adding new, insignificant code.

Some commands such as type or Allo, whose success does not provide the task with new information for the user process, will not have 200 replies returned. If some commands are not supported for the server, return to the confirm completion answer, so that the user process is able to perform the following command. If the request is not an optional implementation of the command, and this command does not implement, it is necessary to return code 502. Here we list each response code in numerical order and its meaning:


Reboot the tag response. In this case the text is OK, it must be: Mark Yyyy=mmmm, where yyyy is the user process data flow token, MMMM is the server tag.


The service is ready in nnn minutes.


Data connection is turned on, ready for delivery


File is in good condition, open data connection


Command succeeded


Command not implemented


System state or system help response


Directory Status


File status


Help information, information only useful to human users


Name System type


Ready for new User Service


The service closes the control connection and can exit the login


Data connection open, no transfer in progress


Data connection turned off, the requested file operation was successful


Enter passive mode


User Login


The requested file operation is complete




User name is correct, requires password


Require account information at logon


Requested file operation requires further command


Cannot provide service, turn off control connection


Cannot Open data connection


Close connection, Abort transmission


The requested file operation was not performed


Abort the requested operation: there is a local error


Operation not performed: System is low on storage space


Bad format, command not recognized


Parameter syntax error


Command not implemented

503 </

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.