As a general rule, the process of designing a Web server is divided into simple steps, each step being implemented as a module.
1. Overall design
The server program sends the file to the client or receives the file from the client, each communication can only do one file transfer, after the transfer is ready to process the next communication. The client program can exit after receiving or sending a file. Therefore, the server short program is a dead loop, after processing once do not exit, continue to listen. The client program handles a connection.
2. Client program Design
The client program has 3 main tasks,
(1) Analysis of commands entered by the user
(2) Make a request to the server based on the command
(3) The result of waiting for the server to return the request
The first task is to parse the command, this FTP program client provides the user with 8 kinds of commands, as shown in the following table:
Commands entered by the user |
Command meaning |
Get |
Getting files from the server |
Put |
Transferring files to the server |
Cd |
Directory to enter the client |
!cd |
Enter the server-side directory |
Ls |
List the contents of the client's current directory |
!ls |
List the contents of the current directory on the server side |
Connect |
Connecting to a server |
Bye |
Exit program |
The client obtains a string from the standard input, parses the string to get commands and arguments, and therefore requires a fixed string input. After the input format is fixed, the client will split the string to determine the function to complete. Each command entered by the user will send a request code to the server through the client, which can be processed by identifying the request code to determine the client's request. The following is a table of commands and corresponding requests entered by the user:
Commands entered by the user |
The corresponding request |
Get |
GET |
Put |
PUT |
Cd |
No need to communicate with server, no request |
!cd |
Cd |
Ls |
No need to communicate with server, no request |
!ls |
Ls |
Connect |
The connection request is made and no additional processing is required on the server side, so no request code |
Bye |
BYE |
3. Server-side programming
The server program is similar to a client program and has 3 tasks.
(1) Analysis Request code
(2) According to the request code to make corresponding processing
(3) Wait for the return result or answer message
According to the design of the top client we know that there are only 5 request codes to be processed on the server side, as shown in the following table:
request code |
corresponding processing |
get |
transfer files to client |
put |
Accept client Files |
cd |
Enter directory |
ls |
|
bye |
disconnect |
After dividing the task, there are two main links need to be clear, is the communication protocol and server model, the communication protocol of this program is divided into two kinds. One is a get command, a put command and a command that!LS commands need to transfer the contents of a file, a communication protocol with a "four-time handshake", and a "two-time handshake" for a command that does not need to transfer the contents of a file.
The server side uses the model of the concurrent server, and if the client is blocked, the server can still handle the other connections.
Web server implementation of File transfer programming