1. Open two ports on the server. One is to listen to commands and the other is to transmit file data.
2. Connect the client to the listening port of the server, send the test connection command, and check whether the server is running normally. If the dialog is normally transferred to step 3, otherwise, terminate the dialog, or reconnect and check.
3. the server sends a confirmation message to the client through the command port to notify the client service to run normally, and checks local settings to determine whether files can be sent to the client. If you can go to the next step, otherwise, the cause of the error is sent to the client through the command port to stop the session. The client processes the received information, terminates all actions, or tries again.
4. the server sends the port number opened locally for file transfer to the client, and then the client connects to the server port through socket. At the same time, the client automatically selects a random port to connect to the server port. (Because in general, the server port should be fixed. In addition, when the socket connection is used, the server port is fixed, and the client port is randomly selected during the connection process. If you want the server to connect to the client, on the one hand, the server needs to develop too many ports, and the selection of ports is also very troublesome; on the other hand, the client may have a lot of restrictions, it needs to be set according to different situations)
5. Send the file to the client through the file transfer port.
6. When transferring a large file, it is generally divided into small pieces for transmission, because during the transfer process, data is stored in the memory, if every file is stored in the memory, memory overflow will soon occur.
7. In addition, multiple processes can be downloaded simultaneously. At this time, the same port is not used, because there are many channels in the same port, each file is transmitted through a channel without interfering with each other.