QT-Network

Introduction:

This article discusses how to communicate via UDP socket in Qt program.

What is UDP?

UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn’t important. Some application-level protocols use UDP because it is more lightweight than TCP. With UDP, data is sent as packets (datagrams) from one host to another. There is no concept of connection, and if a UDP packet doesn’t get delivered successfully, no error is reported to the sender.

What is QUdpSocket ?

The QUdpSocket class allows you to send and receive UDP datagrams. QUdpSocket transfers data as datagrams instead of continuous stream of data. QUdpSocket is a subclass of QAbstractSocket that allows you to send and receive UDP datagrams.

How does it work ?

The most common way to use QUDPSocket class is to bind to an address and port using bind(), then call writeDatagram() and readDatagram() to transfer data.

1. In main(), we create an instance of UDPSocket class:
UDPSocket client;
2. In the constructor, UDPSocket::UDPSocket(), a QUdpSocket will be created:
socket = new QUDPSocket(this);
3. Then, we bind to address and port:
socket->bind(QHostAddress::LocalHost, 1234);
4. In main(), we call UDPSocket::Hello(), and it actually sends data gram:
socket->writeDatagram(Data, QHostAddress::LocalHost, 1234);
5. When the data comes in, we read that datagram in UDPSocket::readyRead():
socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);

Sample Application:

//main.cpp

#include
#include “udp.h”

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UDPSocket client;
client.Hello();
return a.exec();
}
//udp.h
#ifndef UDP_H
#define UDP_H

#include
#include

class UDPSocket : public QObject
{
Q_OBJECT
public:
explicit UDPSocket(QObject *parent = 0);
void Hello();
signals:

public slots:
void readyRead();

private:
QUdpSocket *socket;
};

#endif // UDP_H
//udp.cpp
#include “udp.h”
UDPSocket::UDPSocket(QObject *parent) :
QObject(parent)
{
// create a QUDP socket
socket = new QUdpSocket(this);
// The most common way to use QUdpSocket class is to bind to an address and port using bind()
socket->bind(QHostAddress::LocalHost, 1234);
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}

void UDPSocket::Hello()
{
QByteArray Data;
Data.append(“Hello from UDP”);

// Sends the datagram datagram to the host address and at port.

socket->writeDatagram(Data, QHostAddress::LocalHost, 1234);
}
void UDPSocket::readyRead()
{
// when data comes in
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());

QHostAddress sender;
quint16 senderPort;

// Receives a datagram no larger than maxSize bytes and stores it in data.
// The sender’s host address and port is stored in *address and *port

socket->readDatagram(buffer.data(), buffer.size(),
&sender, &senderPort);
qDebug() << “Message from: ” << sender.toString();
qDebug() << “Message port: ” << senderPort;
qDebug() << “Message: ” << buffer;
}

References

1. http://doc.qt.io/qt-5/qudpsocket.html#details

For any technical support or queries feel free to write to us at sales@e-consystems.com