Implementing Network Communication in Qt with TCP and UDP

TCP Communication in Qt

TCP Programming Charatceristics

  • Requires both server and client components
  • Add QT += network to project file
  • Key classses: QTcpServer and QTcpSocket

TCP Server Implementation

Project Configuration

QT += core gui network

UI Components

  • Read-only receive text area
  • Port number input field
  • Message input field
  • Action buttons (start/stop server, send)

Core Functionality

// Server initialization
NetworkServer::NetworkServer(QWidget *parent) 
    : QWidget(parent)
{
    server = new QTcpServer(this);
    connection = new QTcpSocket(this);
    
    connect(server, &QTcpServer::newConnection,
            this, &NetworkServer::handleNewConnection);
}

void NetworkServer::startListening()
{
    quint16 port = portInput->text().toUShort();
    server->listen(QHostAddress::Any, port);
}

void NetworkServer::handleNewConnection()
{
    connection = server->nextPendingConnection();
    connect(connection, &QTcpSocket::readyRead,
            this, &NetworkServer::processIncomingData);
}

void NetworkServer::processIncomingData()
{
    QString message = QString(connection->readAll());
    receiveDisplay->appendPlainText(message);
}

void NetworkServer::sendMessage()
{
    QByteArray data = messageInput->text().toUtf8();
    connection->write(data);
}

TCP Client Implementation

void NetworkClient::connectToServer()
{
    socket->connectToHost(serverIpInput->text(), 
                         serverPortInput->text().toUShort());
    
    connect(socket, &QTcpSocket::connected,
            this, &NetworkClient::onConnected);
}

void NetworkClient::onConnected()
{
    connect(socket, &QTcpSocket::readyRead,
            this, &NetworkClient::readServerResponse);
}

void NetworkClient::readServerResponse()
{
    QString response = QString(socket->readAll());
    responseDisplay->appendPlainText(response);
}

UDP Communication in Qt

UDP Programmnig Characteristics

  • Connectionless protocol (no client/server distinction)
  • Uses QUdpSocket class

UDP Implementation

void UdpHandler::initializeSocket()
{
    udpSocket = new QUdpSocket(this);
    if(udpSocket->bind(localPortInput->text().toUShort())) {
        connect(udpSocket, &QUdpSocket::readyRead,
                this, &UdpHandler::processDatagrams);
    }
}

void UdpHandler::processDatagrams()
{
    while(udpSocket->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(), datagram.size());
        messageDisplay->appendPlainText(datagram);
    }
}

void UdpHandler::sendDatagram()
{
    QByteArray data = messageInput->text().toUtf8();
    QHostAddress targetAddress(targetIpInput->text());
    quint16 targetPort = targetPortInput->text().toUShort();
    
    udpSocket->writeDatagram(data, targetAddress, targetPort);
}

Tags: Qt QTcpServer QTcpSocket QUdpSocket network-programming

Posted on Fri, 10 Jul 2026 16:59:41 +0000 by Floydian