One of Qt Network Programming

Source: Internet
Author: User
Tags ftp client file transfer protocol ftp protocol

Network Programming directory

* Network programming classes in Qt
* HTTP and FTP high-level network operations
* Use QTcpSocket and QTcpServer for TCP Programming
* Use QUdpSocket for UDP Programming
* Use QHostInfo to parse the Host Name
* Network Proxy Support
* Bearer Management Support



Network Programming
The QtNetwork module provides classes for implementing TCP/IP clients and servers. It provides QFtp classes such as implementing specific application-layer protocols, representing the underlying network protocols: QTcpSocket, QTcpServer and QUdpSocket, and high-level classes that use universal protocols for network operations: QNetworkRequest, QNetworkReply, and QNetworkAccessManager. It also provides classes for bearer management: QNetworkConfigure, QNetworkConfigureManager, and QNetworkSession.



Network programming class in Qt
The following classes are used to support network programming of Qt.




HTTP and FTP high-level network operations
Network Access API is a collection of common Network operation classes. This API is used to obtain and upload data on HHTP, and only exposes general classes, functions, signals, or high-level concepts.
Network requests are presented by the QNetworkRequest class, which is also used as a container containing request-related information, such as any header information and
The encryption method used. When a request object is created, the specified URL can be used to determine the protocol used by the request. Currently, URLs of HHTP, FTP, and local files can be downloaded and uploaded.
The collaborative work of network operations is implemented by the QNetworkAccessManagement class. Once a request is created, this type is used to distribute requests and send signals to report the progress of request processing. Manager is also used together with the use of cookies on clients to store data, authorize requests, and proxies.
The responses to network requests are presented by the QNetworkReply class. When a request is distributed, it is created by QnetworkAccessManager.
The signal provided by QNetworkReply can be used to detect each response separately, or the developer can choose to use the manager signal to achieve this purpose, instead of querying the response information. Because QNetworkReply is a subclass of QIODevice, the response information can be processed synchronously or asynchronously, for example, blocking or non-blocking operations.
Each application or database can create one or more QNetworkAccessManager instances to process network communication.

Use QFtp to write an FTP Client


FTP (file transfer protocol) is a protocol that is usually used to browse remote host directories and file transfers.
FTP uses two network connections, one for transmission commands and the other for data transmission. The FTP protocol has a status, and the client needs to send several commands before data transmission. The FTP client establishes a connection and keeps the connection open through a session. Multiple transmission operations can occur in each session.
QFtp supports FTP client. It has the following features:
* Non-blocking operations. QFtp is asynchronous. You can schedule a series of commands so that a command can be executed after the control returns to the Qt event processing loop.
* Command ID. Each Command has a unique ID. You can use this ID to track the execution of the command. For example, QFtp sends commandStarted () and commandFinished () signals for each executed command using the command ID.
* Indicates the data transmission progress. QFtp sends signals (QFtp: dataTransferProgress (),
QNetworkReply: downloadProgress (), and QNetworkReply: uploadProgress ()). You can connect these signals
QProgressBar: setProgress () or QProgressDialog: setProgress ().
* QIODevice support. This class provides support for downloading and uploading data from QIODevice, and additional QByteArray-based APIs.
There are two ways to use QFtp. The most common method is to keep the TRACE command ID and obtain each
Command execution. Another method is to dispatch all the commands at a time and connect only to the done () signal.
The command is sent only after it is executed. The first method requires more work, but it gives you greater control over the execution of each command, and
You can initialize Subsequent commands Based on the execution result of the previous command. This method also allows you to provide more feedback to users.
The FTP example shows how to write an FTP client. Writing your own FTP (or HTTP) server based on the low-level QTcpSocket and QTcpServer classes is also possible.

Use QTcpSocket and QTcpServer for TCP Programming


TCP (Transport Control Protocol) is the underlying network protocol used by most Internet protocols (including HTTP and FTP) and is mainly used for data transmission. It is a reliable, stream-oriented and connection-oriented transmission protocol. It is particularly suitable for continuous data transmission.
QTcpSocket provides a TCP interface. You can use QTcpSocket to implement standard network protocols, such as POP3, SMTP, NNTP, and custom protocols.
Before data transmission, you must establish a TCP connection to the remote host and port. Once the connection is established, the IP address and port number can both be obtained through QTcpSocket: peerAddress () and QTcpSocket: peerPort. You can close the connection at any time and the data transmission will stop immediately.
QTcpSocket works asynchronously and reports status changes and errors by sending signals. This is similar to QNetworkAccessManager and QFtp. It depends on the data coming from the event loop detection and automatically refreshes the data to be sent. You can write data to the socket through QTcpSocket: write () and read data through QTcpSocket: read. QTcpSocket represents two independent data streams: Read data streams and write data streams.
Because QTcpSocket inherits from QIODevice, you can use it with QTextStream and QDataStream. When reading data from QTcpSocket, you must call QTcpSocket: bytesAvailable () make sure there is enough data to read.
If you need to process the incoming TCP connection (for example, in a server program), use the QTcpServer class. Call QTcpServer: listen () to establish the server and connect to the QTcpServer: newConnection () signal. The signal is sent after each client connects. In your own slot function, use QTcpServer: nextPendingConnection () to accept the connection request and return QTcpSocket to communicate with the client.
Although most of these functions work asynchronously, you can also use QTcpSocket (for example, blocking) in synchronous mode ). To implement the blocking operation, call the waitFor... () function of QTcpSocket, which will suspend the called process to know that the signal is sent. For example, after calling the non-blocking QTcpSocket: connectToHost () function, you can call QTcpSocket: waitForConnected () to block the process and know that the connected () signal is sent.
Synchronous sockets usually make the Code have a very simple control process. The biggest drawback of the waitFor... () method is that the event will not be processed when the waitFor... () function is blocked. If used in the GUI thread, the user interface will be frozen. For this reason, we recommend that you only use synchronous sockets in non-GUI threads. When synchronous sockets are used, QTcpSocket does not need any event loop.
The Fortune Client and Fortune Server examples show how to use QTcpSocket and QTcpServer to write TCP Client-Server-based applications. You can also view the Blocking Fortune Client example to learn how to use the synchronization QTcpSocket in a separate thread (the event loop is not used at this time). Threaded Fortune Server is a multi-thread TCP Server, each active client has only one thread.

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.