FTP client programming structure

Source: Internet
Author: User
Tags ftp client

In fact, the FTP client tool is just to connect to the FTP server with a winsocket, and then send commands like it. In this process, we rely on the sending-response mechanism. Send the FTP command -- receive the returned response information -- analyze the information -- execute related operations -- send the next command. In general, there is a socket used to connect to the port of the FTP server (such as the default port 21), which is responsible for sending and receiving the response information returned by the FTP command. Some operations such as "entering the directory" and "deleting files" can be completed by sending a command using this socket. However, for data transmission operations, we have to rely on another socket to display the remote directory list, upload and download files. Before performing this operation, you must send the PASV command. It returns information starting with 227, which contains six numbers separated by commas (,) in brackets. The first four are server addresses, the key is the last two. The last two digits are multiplied by 256. The result is the port opened on the FTP server for data transmission by the next command. If we get the 227 entering passive mode (256, 0, 1042,), the port number is 4 * + 18 =. We use a socket to connect to this port. Then, we can send the command according to the specific operation (display directory list, Upload File STOR, download RETR. The returned response code starts with 125, that is, when the connection is opened, you can start to transmit data. In this case, you can use the socket sending or receiving method to transmit data. After completion, the server returns code 226 transfer complete, indicating that the data transmission is complete. It is worth noting that we 'd better not send multiple commands at a time. For example, if we want to return to the upper-level directory and display the directory, we have to send cdup, PASV, list, and we cannot send it all at once: cdup/R/N, PASV/R/N, list/R/n. Instead, wait for the response code after sending the cdup, and then send the next one. When PASV returns, we open another socket to connect to the relevant port. Then, send the list. After 125 is returned, the system starts to receive data and returns 226. The process of uploading and downloading is similar (the file size must be obtained before downloading ).

1. The client and server are connected to FTP based on TCP, and the port number is 21. If the client and server are successfully connected, the server returns a string, for example, 220 GMS (Version 5.0)
The first three characters 220 indicate that the client is successfully connected to the server. The subsequent characters may vary with the server, but we only care about the first three characters. 2. user name and password authentication
The client sends the user name to the server, for example, user Guo.
If the server finds the user name Guo, a string is returned, such as 331 user name okay and need password.
Then the client should send the password to the server, for example, pass aaa
The password is correct. The returned result is 230 user logged in, proceed.
If the user name or password fails to pass the authentication, the returned result is 530 not logged in.
The user name can also be anonymous, for example, USER anonymous.
331 anonymous access allowed, send identity (E-Mail Name) as password.3. Create a data connection
FTP uses two connections to exchange data between the client and the server. Just now we established the first connection-control connection, which is used to send commands and responses. We also need to establish the second connection-data connection for data transmission. FTP provides two methods to establish data connections, one is the PORT Command and the other is the PASV command. We use the latter. The client sends the PASV command to the server. The server returns the IP address and port number, which is used to provide the PASV command to the client for data connection. For example: 227 entering passive mode (10,175,125, 9, 17)
The first four digits in the six digits in the brackets indicate the IP address 10.175.125.49, and the last two digits indicate the port number. The two digits are calculated as follows: 9*256 + 17 = 2321
In this way, the client can use the IP address 10.175.125.49 and port number 2321 to connect to the server and establish a data connection. 4. Other related commands and responses
● LIST Command
This command is used to obtain the Directory List of the FTP server. After the server operation is successful, return:
150 opening ASCII mode data connection for/bin/LS.
The directory list needs to be transmitted through a data connection. If the data connection is not ready, the following result is returned:
425 can't open data connection.
After the directory list is uploaded to the client, return:
226 transfer complete. ● CWD command
This command is used to change the working directory of the server. For example, CWD books
The server returns 250 directory changed to/C:/books.
If the directory is not found, the following command is returned: 550/C:/book: no such file or directory. ● RETR.
This command is used for file download. For example, RETR a.txt
Server return value: 150 opening ASCII mode data connection for a.txt (124 bytes ).
226 transfer complete. ● STOR command
This command is used to upload files. For example, STOR B .txt.
Server return: 150 opening ASCII mode data connection for B .txt. The above is the basic collaboration process between the client and the server.

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.