C + + Implementation of FTP breakpoint continued transmission __c++

Source: Internet
Author: User
Tags ftp file win32 ftp client ftp file transfer ftp protocol
C + + implementation of FTP breakpoint continued transmissionFirst, the preface

The continuation of the breakpoint, as the name implies is in the file transfer process due to network outages or other causes of transmission interruption, then the file is only a part of transmission, the next time in the transmission can continue the progress of the transmission. is particularly useful in situations where network conditions are unstable, especially in the Internet environment. The current universal download tool, whether it is HTTP download tools, such as netants, FlashGet, or FTP download tools, such as cuteftp and so on have a breakpoint to continue to pass function. Ii. Selection of programmes

The file transfer task of the quasi-provincial task scheduling system is equivalent to an FTP client that can upload files to the FTP service or download files from an FTP server. As the FTP protocol is already a standard protocol, there are already many mature FTP software, there are many ways to implement the FTP protocol. The main methods of writing FTP client applications using C + + Builder are: Using Microsoft Win32 Internet APIs and using FTP components. 2.1 Microsoft Win32 Internet API

With internet Internet application Support, Microsoft provides WIN32 Internet API function support, which is provided by the WININET.DLL dynamic link function library.

Win32 Internet APIs (that is, WinInet) help enable access to common Internet protocols such as Gopher, FTP, and HTTP. Using WinInet, you can write Internet client applications at a higher programming level without having to deal with the details of Winsock, TCP/IP, and specific Internet protocols. WinInet provides a consistent set of functions for all three protocols and uses the common Win32 API interface. This consistency minimizes the code changes that are required when the underlying protocol changes (for example, from FTP to HTTP).

Using the Win32 Internet API to implement FTP clients has the following advantages:

n reading information from an FTP server is as easy as reading a file from a hard disk.

N Developers who use Win32 Internet functions do not need to be familiar with TCP/IP or Windows Sockets, and for many common operations, developers do not need to know the specifics of the specific protocol they use. However, developers can still program at the socket level directly using the WinSock and TCP/IP protocols.

In the previous quasi-provincial system of FTP file transfer and IC card management system Ftpstation is implemented by this method. However, the following problems were found during use:

N Network condition is not stable, the file is not fully uploaded, but the system still reports file upload success.

N Network condition is unstable, file transfer process is interrupted, task scheduling System File Transfer task is blocked hangs, cause later file cannot upload, can only restart task scheduling system.

n Win32 the Internet API does not provide a function to support breakpoint continuation, if you need to implement this feature, you must program at the Winsock level, and programmers need to know a lot of FTP protocols and programming details. It is difficult to guarantee the reliability of writing the method of continuous transmission. 2.2 FTP Component

The FTP protocol is a standard, mature protocols, many of the current development tools include the FTP components, they are encapsulated at a higher level, providing a wealth of methods and attributes, allowing programmers to easily understand the details of the FTP without the information to develop a powerful FTP client and FTP server software. The C + + Builder 6 contains two components that 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).

1) tnmftp Control

The TNMFTP control supports the FTP protocol, which requires a 32-bit TCP/IP protocol stack WSOCK32 on the machine. Dll. This DLL is available in both WINDOWS95 and later operating systems. The Tnmftp control uses asynchronous working mode, uses event triggering mechanism, provides rich properties and methods, supports proxy and breakpoint continuation, and can use it to develop a powerful FTP client program. The previous data acquisition system (quasi-provincial predecessor) is the use of this component to achieve file transfer. However, in the process of being used, the component is also deficient:

n It uses asynchronous mode of operation, which is especially useful when developing interactive FTP client programs, but in quasi-provincial task scheduling system, FTP tasks are implemented as a DLL, no user interface is provided, and the main program is scheduled to execute without user intervention. If asynchronous working mode is used, complex control is required to ensure the sequential execution of each step.

n the use of the process found that the component has a memory leak, there are many people on the web also pointed out that the component exists Bug,borland company no longer support the component. This problem is very serious for a system that needs to run stably for a long time.

n When using this component, it is found to be unstable when transferring large files, which often occurs when the file size exceeds 100M.

2) Indy Components

The Indy component is a set of open source components that are written in the Delphi language and can be used in C + + Builder and Delphi applications. At present, Indy has launched the 10.0 version, has developed very mature, in the new version of C + + Builder and Delphi are included in this component. The Indy component uses a blocking synchronous mode of operation that provides a set of useful and simple properties and methods, with rich online help and examples. However, an unresolved problem was found during the trial of the component.

n This component does not provide a method to support the continuation of a breakpoint, and when the file transfer is canceled, the previously transmitted content is not actually delivered to the FTP server.

n does not support a proxy server and does not provide a property or method to set the proxy server.

3 ICS (Internet Component Suite)

ICS is also an open source component, written in Delphi, and can be used in various versions of C + + Builder and Delphi. ICS can take either synchronous or asynchronous mode of operation. Each method can work in both synchronous and asynchronous mode. And it provides a method for the continuation of the breakpoint and support for the proxy server. The inconvenient part of using this component is that there is no online help, but it provides a rich sample program that can look at the source code when necessary to understand how each method is used.

Iii. Design of the project

Let ftp file transfer with a breakpoint extension function, in addition to the client needs to adopt a certain programming skills, the FTP server must also support the continuation of the breakpoint. In this scenario, the implementation of the client is mainly discussed and should be masked if the server does not support the continuation of the breakpoint. 3.1 Data structure and key classes

The Tftpclient class in the ICS component provides a restget and restput method for continuous transmission (upload and download), respectively. However, before using both methods, you must first specify where to resume from. That is, we need to record the location of the breakpoint, that is, how many bytes were last uploaded and then the transfer was interrupted. Therefore, the following data structure is defined to hold the file transfer information, as shown in Figure 1:

Figure 1 File transfer information class diagram

which

U ID: Unique identification, no practical significance.

U host:ftp server name or IP address.

U remotedir:ftp the server's working path.

U remotefilename:ftp the file name on the server.

U localfilename: The name of the file locally, including the path.

U FileSize: The number of bytes in the last file transfer.

U Direction: Transmission direction, 0-upload, 1-download.

U Flag: Normal exit flag, 0-Interrupt, 1-abnormal interrupt. The key to breaking a breakpoint is to know that the last file transferred bytes, but if the program exits unexpectedly, it may not be able to correctly record the last bytes transferred, then you need to start from the beginning.

File transfer records are saved in the file. Therefore, the class also provides the following methods:

U Serialize: Serialization method that serializes file transfer information into a file stream.

U gettransferinfos: Load file transfer record from file to list, each item in the list is a transmission record.

The quasi-provincial task scheduling system starts the file transfer task every day, and if every file transfer is recorded, the file becomes larger. Therefore, if the file is transferred, the record is removed from the list and only files that need to be renewed are recorded in the file. 3.2 Flow chart

Figure 2 File Transfer flowchart

Note: The transfer process may terminate during file transfer due to network or human termination. The OnProgress event of the Tftpclient component is triggered during file transfer, which has parameters indicating the number of bytes currently transferred

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.