I. Introduction
After the breakpoint renewal, as the name suggests that the transmission is transmitted during the file transfer or other reasons, when the file only transmits a part, the previous transmission schedule can be continued in the next transmission. It is especially useful in network conditions instability, especially in the Internet environment. The current general download tool, whether it is HTTP download tool, such as Netants, Flashget, etc., or FTP download tool, such as CuteFTP, etc.
Second, the program choice
Quasi-provincial task scheduling system file transfer tasks equivalent to an FTP client, can upload files to the FTP service, or download files from the FTP server. Since the FTP protocol is already a standard protocol, there are many mature FTP software, and there are many ways to implement the FTP protocol. Write an FTP client application using C Builder mainly: Use Microsoft Win32 Internet API and use FTP components.
2.1 Microsoft Win32 Internet API
On Internet Internet Application Support, Microsoft provides the Win32 Internet API function support, which is provided by the Wininet.dll dynamic link function library.
Win32 Internet API helps access to common Internet protocols such as Gopher, FTP, and HTTP. Use Wininet to write Internet client applications at higher programming levels without having to process details of Winsock, TCP / IP, and specific Internet protocols. Wininet provides a consistent function for all three protocols and uses commonly used Win32 API interfaces. This consistency is minimized when the basic protocol changes (eg, from FTP to HTTP).
Using the Win32 Internet API to implement the FTP client with the following advantages:
n Read information from the FTP server is as easy as the hard disk read files.
n Developers using Win32 Internet functions do not need to be familiar with TCP / IP or Windows Sockets. For many common operations, developers do not need to know the details of the specific protocol they use. But developers can still use the Winsock and TCP / IP protocols to program in the socket level,
The FTP file transfer and IC card management system ftpstation in the previous quasi-provincial system are implemented in this way. But found the following issues during use:
n The network condition is unstable, the file is not fully uploaded, but the system still reports the file upload success.
N network conditions are unstable, the file transfer process is interrupted, the file transfer task of the task scheduling system is blocked, causing future files that cannot be uploaded, and can only restart the task scheduling system.
The N WIN32 Internet API does not provide a function of the breakpoint resume. If this feature needs to be implemented, it must be programmed at the Winsock level, and the programmer needs to know a lot of FTP protocols and programming details. Write a renewal method, and the reliability is difficult to guarantee.
2.2 FTP components
The FTP protocol is a standard, mature protocol. At present, there are currently FTP components in many development tools, which are encapsulated at a higher level, providing a wide range of methods and properties, making programmers can do not need to understand FTP details. Easily develop powerful FTP clients and FTP server software. Two components in C Builder 6 can be used to develop FTP applications: TNMFTP and INDY. In addition, there are many mature third-party components such as ICS (Internet Component Suite, etc.). 1) TNMFTP control
The TNMFTP control supports the FTP protocol, using this component to require 32-bit TCP / IP protocol stack WSOCK32.DLL on the machine. There is this DLL in Windows95 and later operating systems. TNMFTP control uses asynchronous mode of operation, using event triggers, and provides rich attributes and methods, support proxy and breakpoint resume, can be used to develop a powerful FTP client program. The previous data collector system (quasi-provincial predecessor) is to use the component to implement file transfer. However, in the process of use, there is still a short condition:
n is especially useful when developing interactive FTP client programs, but in the quasi-provincial task scheduling system, the FTP task is implemented as a DLL, does not provide the user interface, and executed by the main program, No user intervention is required. If an asynchronous mode of operation is used, complex control is required to ensure the order of each step.
n During use, there are problems with memory leaks in the use. There are many people in the network that it is pointed out that the component has bugs, and Borland no longer supports the component. This problem is very serious for systems that require long-term stable operation.
N When using this component, it is discovered that the transmitted large file is unstable, and when the file size exceeds 100m, the transmission interrupt often occurs.
2) INDY components
The INDY component is a set of open source components, written in Delphi language, available for C Builder and Delphi applications. The current INDY has been launched 10.0, which has been developed very mature, and this component is included in the new version of C Builder and Delphi. The Indy component uses blockage synchronous mode of operation, provides a set of practical and simple properties and methods, and has a wealth of online help and examples. But in the process of trying this component, it is found that there is a problem that can be solved.
n The component does not provide a method of supporting breakpoints, and when the file is canceled, the previously transmitted content is not truly transmitted to the FTP server.
n Does not support the proxy server, the properties or methods of setting the proxy server are provided.
3) ICS (Internet Component Suite)
ICS is also an open source component that is written in the Delphi language, which can be used in the various versions of C Builder and Delphi. ICS can use synchronous mode of operation, or asynchronous mode of operation. Each method can work in synchronous and asynchronous mode. And it provides a method of breakpoint and support for proxy servers. The inconvenience to use this component is that there is no online help, but it provides a rich sample program, and you can check the source code if necessary. How to use each method.
Third, program design
Let the FTP file transfer have a break-to-loop transmission function, in addition to the client needs to use certain programming skills, the FTP server must also support breakpoints. In this scenario, the client's implementation method is mainly discussed. If the server does not support breakpoints, the function should be masked.
3.1 Data structure and key class
The TFTPCLIENT class in the ICS component provides RESTGET and RESTPUT methods, which are used to renew (upload and download). But before using these two methods, you must first specify which location is resumed. That is to say, we need to record the location of the breakpoint, that is, how many bytes will be transmitted after the last upload. So define the following data structure to save file transfer information, as shown in Figure 1: Figure 1 File Transfer Information Class
among them:
u id: unique identifier, no practical meaning.
u Host: FTP server name or IP address.
u RemoteDir: The working path of the FTP server.
u RemoteFileName: The file name on the FTP server.
u LocalFileName: The file name on the local, contains the path.
u FileSize: The number of bytes transmitted last time.
u Direction: Transfer Direction, 0 - Upload, 1 - Download.
u FLAG: Normal exit sign, 0- Interrupt, 1-abnormal interruption. The key to the sequel is that the last file is transmitted, but if the program is abnormal exits, the number of bytes of the last transfer may not be recorded correctly, and it is necessary to start transmission from the head.
File Transfer Records are saved in the file. Therefore, the class also provides the following methods:
u Serialize: Serialization method, transfer file information sequence into the file stream.
u GetTransferinfos: Load file transfer from the file records into the list, each of the linked lists are a transfer record.
The provincial task scheduling system will start the file transfer task every day. If each file transfer is recorded, this file will get more and more. Therefore, if the file transfer is completed, the record is removed from the linked list, and only files that need to be subsequently recorded in the file.
3.2 flow chart
Figure 2 File Transfer Flow Chart Note: This time the transmission process will terminate due to the network reasons or artificial termination during file transfer. The ONPRESS event of the TFTPClient component is triggered during the file transmission, which has parameters indicating that the number of bytes currently transmitted.