Reprinted: http://www.ibm.com/developerworks/cn/linux/l-cn-socketftp/index.html#FTP
Introduction:FTP clients such as flashfxp and file Zilla are widely used. In principle, they are implemented using underlying sockets. Two sockets must be established for data exchange between the FTP client and the server, one as the Command Channel and the other as the data channel. The former is used by the client to send commands to the server, such as logging on to and deleting a file, and the latter is used to receive data, such as downloading or uploading a file. This article describes in detail how to call the system interface to send FTP commands to implement FTP client functions such as file upload and download, so that readers can have an in-depth understanding of the principles of the FTP client.
FTP Overview
The file transfer protocol (FTP), as the transfer protocol for shared network files, is widely used in network applications. FTP aims to improve file sharing and reliable and efficient data transmission.
When transferring files, the FTP client first establishes a connection with the server and then sends commands to the server. The server returns a response after receiving the command and executes the command. The FTP protocol has nothing to do with the operating system. Any program on the operating system can transmit data to each other as long as it complies with the FTP protocol. Based on the Linux platform, this article gives a detailed explanation of the implementation principles of the FTP client and explains how to use C to compile a simple FTP client.
FTP protocol
Compared with other protocols, such as HTTP, FTP is more complex. The difference from General C/S applications is that General C/S applications only establish a socket connection, which simultaneously processes connection commands and data transmission between the server and client. In the FTP protocol, commands and data are transmitted separately to Improve the efficiency.
FTP uses two ports, one data port and one command port (also called control port ). These two ports are generally 21 (command port) and 20 (data port ). The control socket is used to send commands, and the data socket is used to transmit data. After each FTP command is sent, the FTP server returns a string containing a response code and instructions. The return code is mainly used to determine whether the command is successfully executed.
Command Port
Generally, the client has a socket used to connect to the relevant port of the FTP server, which is responsible for sending and receiving the response information returned by the FTP command. Some operations such as "Log on", "Change directory", and "delete file" can be completed by sending commands through this connection.
Data port
For data transmission operations, we need to rely on another socket to display the directory list, upload and download files.
If the passive mode is used, the server usually returns a port number. The client needs to open another socket to connect to this port, and then we can send commands according to the operation, data will be transmitted through a new port.
If active mode is used, the client usually sends a port number to the server and listens on this port. The server needs to connect to the data port opened by the client for data transmission.
Active mode (port)
In active mode, the client randomly opens a port greater than 1024 to the command port P of the server, that is, port 21, initiates a connection, and opens port n + 1 listening, the "port n + 1" command is sent to the server. The server actively connects to the specified data port (n + 1) of the client from its own data port (20 ).
The FTP client only tells the server its own port number to allow the server to connect to the specified port of the client. For the client's firewall, this is an external to internal connection and may be blocked.
Passive mode (PASV)
To solve the problem that the server initiates a connection to the customer, another FTP connection method is available, that is, passive connection. Both the command connection and data connection are initiated by the client, which solves the problem that the connection from the server to the client's data port is filtered by the firewall.
In passive mode, when an FTP connection is enabled, the client opens two arbitrary local ports (n> 1024 and n + 1 ).
The first port connects to port 21 of the server and submits the PASV command. Then, the server opens an arbitrary port (P> 1024) and returns227 entering passive mode)". It returns information starting with 227, which contains six numbers separated by commas (,), the first four are the server addresses, and the last two, add the last number to the second, which is the port opened by the FTP server for data transmission. If you get the 227 entering passive mode (H1, H2, H3, H4, P1, P2), the port number is
P1 * 256 + P2, IP Address: h1.h2. h3.h4. This means that a port is opened on the server. After receiving the command, the client connects to port P of the server through port n + 1 and transmits data between the two ports.
FTP commands
Each FTP command consists of 3 to 4 letters. The command is followed by a parameter and separated by spaces. Each Command ends with "\ r \ n.
To download or upload a file, you must first log on to the FTP server, then send the command and exit. In this process, the main Commands used include user, pass, size, rest, CWD, RETR, PASV, port, and quit.
User: Specifies the user name. It is usually the first command issued after controlling the connection. "User gaoleyi \ r \ n": the user name is gaoleyi.
Pass: Specifies the user password. This command follows the USER command. "Pass gaoleyi \ r \ n": the password is gaoleyi.
Size: the size of the specified file returned from the server. "Size file.txt \ r \ n": If the file.txt file exists, the size of the file is returned.
CWD: change the working directory. For example, "CWD dirname \ r \ n ".
PASV: enables the server to listen on the data port and enters the passive mode. For example, PASV \ r \ n ".
Port: indicates the port number listened by the FTP Server Client, allowing the FTP server to actively connect to the client. For example, "port H1, H2, H3, H4, P1, P2 ".
RETR: download an object. "RETR file.txt \ r \ n": the downloaded file file.txt.
Stor: upload a file. "Stor file.txt \ r \ n": Upload the file file.txt.
Rest: This command does not transmit files, but skips the data after a specified vertex. This command should be followed by other FTP commands that require file transfer. "Rest 100 \ r \ n": Specify the offset of file transfer to 100 bytes.
Quit: Close the connection to the server.
FTP Response Code
After the client sends the FTP command, the server returns the response code.
The response code is encoded in three digits:
The first number provides a general indication of the command status, such as success, failure, or incomplete response.
The second number is the type of response. For example, 2 indicates the connection-related response, and 3 indicates user authentication.
The third number provides more detailed information.
The meaning of the first digit is as follows:
1 indicates that the server has correctly received the information and has not yet processed it.
2 indicates that the server has correctly processed the information.
3 indicates that the server receives the correct information and is processing it.
4 indicates that the information is temporarily incorrect.
5 indicates that the information is permanently incorrect.
The second number has the following meanings:
0 indicates the syntax.
1 indicates the system status and information.
2 indicates the connection status.
3 indicates information related to user authentication.
4 indicates that it is not defined.
5 indicates information related to the file system.