Qt Network (3) FTP (1)

Source: Internet
Author: User
Tags file transfer protocol

In the previous section, we talked about HTTP programming. This section describes FTP programming and similar FTP programming. FTP is the file transfer protocol, that is, the file transfer protocol. The main function of FTP is to allow users to connect to a remote computer, view the files on the remote computer, and then copy the files from the remote computer to the local computer, or, you can send files from a local computer to a remote computer.

In QT, we can use the qnetworkaccessmanager and qnetworkreply classes described in the previous section to write FTP programs, because they are easy to use. However, for complicated FTP operations, QT also provides the qftp class. Using this class, we can easily write an FTP client program. Next we will first view this class in help.

 

In qftp, all operations correspond to a specific function, which can be called as commands. For example, connecttohost () connects to the server command, login () logon command, get () download command, mkdir () new directory command, and so on. Because the qftp class works asynchronously, all these functions are not blocking functions. That is to say, if an operation cannot be executed immediately, this function will be directly returned until the control of the program returns the QT event loop for actual execution, which will not affect the display of the interface.

All commands return an int-type number, which allows us to track this command and view its execution status. When each command starts to be executed, a commandstarted () signal is sent. When the command is executed, a commandfinished () signal is sent. We can use these two signals and command numbers to obtain the command execution status. Of course, we don't want to write down the number of each command, so we can also use currentcommand () to get the current command, and its return value corresponds to the command.

 

Next we will first look at a simple FTP client example and then extend it.

In this example, a file is downloaded from the FTP server and displayed.

1.Create a qt4 GUI qapplication.

Project name: "myftp", select the qtnetwork module, and then select qwidget as the base class.

2.Modify widget. UIFile.

Add a text browser and a label. The effect is as follows.

 

3.In Main. cpp.

To use Chinese characters in the program, we add the header file # include <qtextcodec> in Main. cpp.

Add the code in the main () function: qtextcodec: setcodecfortr (qtextcodec: codecforlocale ());

4.In the widget. h.

First add the header file: # include <qftp>

Then define the object in private: qftp * FTP;

Add a private slot function:

Private slots:

Void ftpcommandstarted (INT );

Void ftpcommandfinished (INT, bool );

5.In the widget. cpp.

(1) Add code to the constructor:

FTP = new qftp (this );

FTP-> connecttohost ("ftp.qt.nokia.com"); // connect to the server

FTP-> login (); // log on

FTP-> Cd ("QT"); // jump to the "QT" Directory

FTP-> get ("Install"); // download the "Install" File

FTP-> close (); // close the connection

Connect (FTP, signal (commandstarted (INT )),

This, slot (ftpcommandstarted (INT )));

// Send a signal when each command starts to be executed

Connect (FTP, signal (commandfinished (INT, bool )),

This, slot (ftpcommandfinished (INT, bool )));

// Send a signal when the execution of each command ends

We performed several FTP operations in the constructor, logged on to the site, and downloaded a file. Then we associate two signals and slots to track command execution.

(2) Implementation of Slot functions:

Void Widget: ftpcommandstarted (INT)

{

If (ftp-> currentcommand () = qftp: connecttohost ){

UI-> label-> settext (TR ("connecting to server ..."));

}

If (ftp-> currentcommand () = qftp: Login ){

UI-> label-> settext (TR ("logging ..."));

}

If (ftp-> currentcommand () = qftp: Get ){

UI-> label-> settext (TR ("download ..."));

}

Else if (ftp-> currentcommand () = qftp: Close ){

UI-> label-> settext (TR ("Closing connection ..."));

}

}

Each time a command is executed, the ftpcommandstarted () function is executed. It has an int ID, which is the ID returned when the command is called, for example, int loginid = FTP-> login (); in this case, we can use if (ID = loginid) to determine whether the login () function is executed. However, we do not want to set a variable for each command to store its return value. Therefore, we use ftp-> currentcommand () to obtain the type of the currently executed command. In this function, different status information is displayed when different commands are started.

Void Widget: ftpcommandfinished (INT, bool error)

{

If (ftp-> currentcommand () = qftp: connecttohost ){

If (error) UI-> label-> settext (TR ("An error occurred while connecting to the server: % 1"). Arg (ftp-> errorstring ()));

Else UI-> label-> settext (TR ("successfully connected to the server "));

}

If (ftp-> currentcommand () = qftp: Login ){

If (error) UI-> label-> settext (TR ("Logon error: % 1"). Arg (ftp-> errorstring ()));

Else UI-> label-> settext (TR ("Logon successful "));

}

If (ftp-> currentcommand () = qftp: Get ){

If (error) UI-> label-> settext (TR ("Download error: % 1"). Arg (ftp-> errorstring ()));

Else {

UI-> label-> settext (TR ("downloaded "));

UI-> textbrowser-> settext (ftp-> readall ());

}

}

Else if (ftp-> currentcommand () = qftp: Close ){

UI-> label-> settext (TR ("Connection closed "));

}

}

This function is similar to the ftpcommandstarted () function, but it is executed at the end of a command execution. It has two parameters. The first int ID is the number returned when the command is called. We have discussed it above. The second is bool error, which indicates whether the current command has encountered an error. If an error occurs, the error is true. Otherwise, the error is false. We can use it to output error messages. In this function, different status information is displayed when a command is completed, and possible error information is displayed. In if (ftp-> currentcommand () = qftp: Get), when the download is completed, let textbrowser display the download information.

6.Run the program as follows.

Logon status.

 

After the download is complete.

 

7.Error demonstration.

The following is an example of an error.

Change the code FTP-> login () in the constructor to FTP-> login ("Tom", "123456 ″);

Then run the program:

 

As you can see, it outputs an error message indicating the wrong command and error content. In fact, we also want to tell you this error. If the user name and password are not set in FTP, the default user name should be anonymous, and the password can be entered at will, other user names may cause errors.

In the next section, we will expand this program so that it can browse and download all the files on the server.

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.