A class that implements FTP breakpoint resume

xiaoxiao2021-03-06  63

This article is built on your base for Socket knowledge (there is a little bit enough :))

The FTP client implements two channels, a control command channel, so that the FTP server knows what the client is to do, a data transfer channel. The so-called two channels are only two connections that call the Connect function, just the control command channel is specifically used to transmit some string command information, and the data channel is used to transfer files. The control command channel must be connected by the client to the server (the default port is 21, or the port can be specified, which is to see the server settings). The process of connecting has completed the login of FTP. The data channel is not necessarily, which one is even, please see the explanation of the PASV command.

In fact, the principle of FTP breakpoint is simple, can be divided into breakpoint downloads and breakpoint upload.

The implementation steps of the client are as follows:

First, download:

1. Send the "REST local file length" command to the server, tell the server, and the client has to download it. At this time, the server still doesn't know which file is downloaded;

2. Send the "Retr File Name" command to the server to inform the server to download the file name to download, when the server starts the file pointer read file and sends data.

3, the client positions the local file pointer (the end of the file);

4, the preparations of the two ends have been completed, the client creates a socket to establish a data channel passive or non-passive mode, and the recirculation calls the RECV to receive data and chase the local file;

Second, upload:

1. Get the same name file size on the server and local to upload files;

2. Send "APPE File Name" to the server, notify the server: Next, the data sent from the data channel to you should be attached to the end of this file.

3, position local file pointers (location with file size on FTP)

4, read data from the file pointer and send it. Ok, the principle of FTP breakpoint resume is so simple. Put the breakpoint upload and breakpoint download in the code to the same function (MoveFile), by the GET parameter, is uploaded or downloaded. Of course, the implementation of the entire FTP class has more than 800 rows, including login, exit, get the FTP file size, delete files, response servers, parsing response information, etc. of FTP servers. There is a corresponding annotation code, here is not one.

Here, you focus on the PASV mode, that is, passive mode, which is one of the ftp commands that is not easy to understand, this command request server creates a monitor socket, server creation of a port (non-FTP default port or control command port) The port number returns to the client by the control command channel. After getting this port number, the client can create a new socket (data channel) Connect, and perform file transfer. Otherwise, if it is a non-passive mode, then the listened socket is created by the client, and the server connect is coming.

There is such a understanding of the existence of this command, there is such a situation: the client's IP is an intranet IP. The server's IP is an external network. When the data is transmitted, the internal network IP is invisible to the server. Only the data channel can only be created by the server to start listening to Socket, so it must be performed in passive mode. :) In addition, not all FTP servers support breakpoints, but most of the server has this function, and for the server code that does not support breakpoints, it is deleted local files. download again. So this is affordable. Click to download source code

转载请注明原文地址:https://www.9cbs.com/read-85594.html

New Post(0)