Implementing TCP/IP Network Communication with Qt C++

TCP and UDP Protocols

TCP (Transmission Control Protocol) is a connection-oriented transport layer protocol that ensures reliable communication. It guarantees data integrity, preventing loss, disorder, duplication, or corruption during transmission.

Typical use cases for TCP include:

  • User authentication and session management in applications like instant messengers.
  • Scenarios requiring high reliability and the transfer of large data volumes.

TCP achieves reliability through:

  • A three-way handshake for connection establishment and a four-way handshake for termination.
  • Sequence and acknowledgment numbering mechanisms.
  • Retransmission mechanisms for lost or erroneous packets.

UDP (User Datagram Protocol) is a connectionless, unreliable protocol. It does not establish a connection before sending data, which allows for faster transmission with lower overhead, albeit without delivery guarantees.

UDP is suitable for:

  • Transmitting small data packets (e.g., DNS queries).
  • Real-time text, audio, or video communication in applications like messaging and VoIP.
  • Streaming media services where timeliness is prioritized over perfect reliability.

IP Addresses and Ports

An IP address uniquely identifies a host on a network. Communication requires a valid IP address, which can be 32-bit (IPv4) or 128-bit (IPv6). IPv4 addresses are commonly represented in dotted-decimal notation (e.g., 192.168.1.1).

Port numbers distinguish between different applications or services on the same host. Both TCP and UDP use independent port spaces.

  • Ports are 16-bit integers (0-65535).
  • Well-known ports: 0-1023 (reserved for system services).
  • Registered ports: 1024-49151.
  • Dynamic/private ports: 49152-65535.

Project Configuration

To use Qt's networking features, add the network module to your project file (*.pro):

QT += network

Key classes for TCP communication are:

  • QTcpServer: Represents a TCP server. It inherits from QObject and does not provide direct I/O capabilities.
  • QTcpSocket: Represents a TCP socket connection. It inherits from QIODevice, enabling read and write operations.

Server Implementation

Create a server instance:

QTcpServer *serverInstance = new QTcpServer(this);

Start listening for incoming connections:

bool listening = serverInstance->listen(QHostAddress::Any, 8887);
if (listening) {
    qDebug() << "Server started on port 8887";
} else {
    qDebug() << "Failed to start server";
}

Check the server's listening status:

bool active = serverInstance->isListening();

Stop the server:

serverInstance->close();

Handle new client connections via the newConnection signal:

connect(serverInstance, &QTcpServer::newConnection, this, &MyClass::handleNewClient);

In the slot, accept the pending connection:

void MyClass::handleNewClient() {
    QTcpSocket *clientSocket = serverInstance->nextPendingConnection();
    QHostAddress clientAddr = clientSocket->peerAddress();
    quint16 clientPort = clientSocket->peerPort();
    qDebug() << "New connection from:" << clientAddr.toString() << ":" << clientPort;
    // Setup further communication with clientSocket
}

Client Implementasion

Create a client socket:

QTcpSocket *clientSocket = new QTcpSocket(this);

Connect to a server:

clientSocket->connectToHost("192.168.1.100", 8887, QIODevice::ReadWrite);

Data Transmission

Use QTextStream for convenient text-based I/O over the socket:

QTextStream socketStream(clientSocket);
socketStream << "Hello Server!" << Qt::endl;

To read incoming data, connect to the readyRead signal:

connect(clientSocket, &QTcpSocket::readyRead, this, &MyClass::readSocketData);

Implement the slot to process data:

void MyClass::readSocketData() {
    QTextStream stream(clientSocket);
    QString message = stream.readAll();
    qDebug() << "Received:" << message;
}

Tags: C++ Qt networking TCP/IP Socket Programming

Posted on Tue, 08 Sep 2026 16:31:36 +0000 by alecodonnell