Reprinted from: http://www.ibm.com/developerworks/cn/linux/l-cn-socketftp/index.html#FTP
This is my own client program: http://download.csdn.net/detail/sphone89/4060069
Several important steps of socket programming
The main steps of Socket Client programming are as follows:
1. socket () creates a socket
2. Connect () to the server
3. Write () and read () sessions
4. Close () to close the socket
The main steps of socket server programming are as follows:
- Socket () to create a socket
- BIND ()
- Listen () Listener
- Accept () receives connection requests
- Write () and read () sessions
- Close () Close socket
Implement FTP client upload/download
1. Establish a socket connection between the client and the FTP server.
2. Send the user and PASS commands to the server to log on to the FTP server.
3. Run the PASV command to obtain the port number of the server listener and establish a data connection.
4. Use the RETR/STOR command to download/upload files.
5. After the download is complete, disconnect the data connection and send the quit command to exit.
Establish a socket connection between the client and the FTP server
After the client establishes a connection with the server, the server returns the 220 response code and some welcome information.
Figure 1. Connect the client to the server
Listing 1. Connecting the client to the FTP server and receiving welcome information
Socket control_sock;
Struct hostent * HP;
Struct sockaddr_in server;
Memset (& server, 0, sizeof (struct sockaddr_in);/* initialize socket */
Control_sock = socket (af_inet, sock_stream, 0 );
HP = gethostbyname (SERVER_NAME );
Memcpy (& server. sin_addr,
HP-> h_addr, HP-> h_length );
Server. sin_family = af_inet;
Server. sin_port = htons (port );
/* Connect to the server */
Connect (control_sock, (struct sockaddr *) & server, sizeof (server ));
/* The client receives some welcome information from the server */
Read (control_sock, read_buf, read_len );
Log on to the FTP server from the client
When the client sends the user name and password, the server returns a 230 response code after the verification is passed. Then the client can send commands to the server.
Figure 2. Client logon to FTP Server
List 2. The client sends the user name and password and logs on to the FTP server.
/* Command"User Username \ r \N "*/
Sprintf (send_buf, "user % s \ r \ n", username);/* the client sends the user name to the server */
Write (control_sock, send_buf, strlen (send_buf ))
;/* The response code and information of the client receiving server, which is"331 user name Okay, need password ."
*/
Read (control_sock, read_buf, read_len );
/* Command"Pass password \ r \ n"*/
Sprintf (send_buf, "Pass % s \ r \ n", password );
/* The client sends the password to the server */
Write (control_sock, send_buf, strlen (send_buf ));
/* The response code and information of the client receiving server. Normally"230 user logged in, proceed ."*/
Read (control_sock, read_buf, read_len );
Allow the FTP server to enter passive mode on the client
Before the client downloads or uploads a file, it must first send a command to make the server passive. The server opens the data port and listens. And return the response code 227 and the data connection port number.
Figure 3. The client allows the server to enter the passive mode
Listing 3. Set the server to passive mode and listen on the data port
/* Command"PASV \ r \ n"*/
Sprintf (send_buf, "PASV \ r \ n ");
/* The client tells the server to use the passive mode */
Write (control_sock, send_buf, strlen (send_buf ));
/* The response code of the client receiving server and the new port number. * normally"227 entering passive mode (<H1, H2, H3, H4, P1, P2> )"*/
Read (control_sock, read_buf, read_len );
The client downloads files in passive mode.
When the client sends a command to download the file. The server returns response code 150 and sends the file content to the data connection.
Figure 4. The client downloads files from the FTP server
The client exits the server.
After the client download is complete, send the command to exit the server and close the connection. The server returns the response code 200.
Figure 5. The client exits from the FTP server
First, when the server is ready, 220 is returned. After receiving the response code returned by the server, the client sends the "User username" and "pass password" commands to log on. Then, the response code returned by the server starts with 230, indicating that the client has logged on. In this case, the client sends the PASV command to allow the server to enter the passive mode. If the server returns "227 entering passive mode (, 0,)", the client obtains the port number from it and then connects to the data port of the server. Next, the client sends the download command, the server returns the response code 150, and sends data from the data port. Finally, the server returns
"226 transfer complete" indicates that the data transmission is complete.
Note that the client should not send multiple commands at a time. For example, to open a directory and display the directory, we must send CWD dirname, PASV, list. Wait for the response code after sending the CWD dirname, 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.
During the transmission of multiple files, it should be noted that each new transmission must re-use PASV to obtain the new port number. After receiving the data, the data connection should be closed, in this way, the server returns a 2XX success response. Then the client can continue to transfer the next file.
Compared with downloading files, the login authentication and passive switching modes are the same. You only need to change the commands sent to the server and send the file content through a data connection.