7. QT C++ Network Programming

Qt is provided with an extensive set of network classes to support both client-based and server side network programming. In this tutorial we will see how to develop a client-server network application using QT C++. After viewing the video tutorial, download the source code and try to modify the code, so that, you get an idea of what is discussed and learned in the tutorial video.

The network module in Qt provides some features, such as support for internationalized domain names, better IPv6 support, and better performance. And since Qt 5 allows us to break binary compatibility with previous releases, we took this opportunity to improve the class names and API to make them more intuitive to use.

General Overview


The network module in Qt brings the following benefits:

  • The Qt 5 network classes have more intuitive names and APIs. For example, QServerSocket has been renamed QTcpServer.
  • The entire network module is reentrant, making it possible to use them simultaneously from multiple threads.
  • It is now possible to send and receive UDP datagrams and to use synchronous (i.e., blocking) sockets without having to use a low-level API (QSocketDevice in Qt 3).
  • QHostAddress and QHostInfo support internationalized domain names (RFC 3492).
  • QUrl is more lightweight and fully supports the latest URI specification draft.
  • UDP broadcasting is now supported.

The Qt 5 network module provides fundamental classes for writing TCP and UDP applications, as well as higher-level classes that implement the client side of the HTTP and FTP protocols.

Here's an overview of the TCP and UDP classes:

  • QTcpSocket encapsulates a TCP socket. It inherits from QIODevice, so you can use QTextStream and QDataStream to read or write data. It is useful for writing both clients and servers.
  • QTcpServer allows you to listen on a certain port on a server. It emits a newConnection() signal every time a client tries to connect to the server. Once the connection is established, you can talk to the client using QTcpSocket.
  • QUdpSocket is an API for sending and receiving UDP datagrams.

QTcpSocket and QUdpSocket inherit most of their functionality from QAbstractSocket. You can also use QAbstractSocket directly as a wrapper around a native socket descriptor.

By default, the socket classes work asynchronously (i.e., they are non-blocking), emitting signals to notify when data has arrived or when the peer has closed the connection. In multithreaded applications and in non-GUI applications, you also have the opportunity of using blocking (synchronous) functions on the socket, which often results in a more straightforward style of programming, with the networking logic concentrated in one or two functions instead of spread across multiple slots.

QFtp and QNetworkAccessManager and its associated classes use QTcpSocket internally to implement the FTP and HTTP protocols. The classes work asynchronously and can schedule (i.e., queue) requests.

The network module contains four helper classes: QHostAddress, QHostInfo, QUrl, and QUrlInfo. QHostAddress stores an IPv4 or IPv6 address, QHostInfo resolves host names into addresses, QUrl stores a URL, and QUrlInfo stores information about a resource pointed to by a URL, such as the file size and modification date. (Because QUrl is used by QTextBrowser, it is part of the QtCore library and not of QtNetwork.)

 Click below to see the Video and the source.



 QT C++ Network Programmingusing a Hello World Example


Download Source Code