At the beginning of learning the development method of combining C + + and QT, some good tutorials will be moved to their own blog to facilitate future search and learning. This time, a detailed tutorial and code example of udp module are reproduced;
UDP(User Datagram Protocol) is a lightweight, unreliable, datagram oriented and connectionless protocol. It can be used in places with low reliability requirements. UDP does not need to establish a connection to send and receive data. It only needs to bind the IP address and port number. The sending data is sent directly, and the receiving data needs to cycle to judge whether the data is accepted or not. Qt's signal slot mechanism well realizes the trigger processing function of sending signal after receiving data.
QUdpSocket class is used for UDP communication. It inherits from QAbstractSocket, so it shares most of the interface functions with QTcpSocket. The main difference is that QUdpSocket transmits data in datagrams rather than continuous data streams. The function QUdpSocket::writeDatagram() is used to send datagrams. The length of datagrams is generally less than 512 bytes. Each datagram contains the IP address and port information of the sender and receiver.
To receive UDP data, bind a port with the QUdpSocket::bind() function to receive incoming datagrams. When a datagram is passed in, it will send a readRead() signal and use the readDatagram() function to read the received datagram.
UDP message transmission includes unicast, broadcast and multicast.
Unicast mode: datagrams sent by one UDP client are only sent to another UDP client with specified address and port, which is one-to-one data transmission;
Broadcast mode: datagrams sent by one UDP client can be received by all other UDP clients in the same network. QUdpSocket supports IPv4 broadcasting. Broadcast is often used to implement network discovery protocols. To obtain broadcast data, you only need to send the datagram to the specific receiver address QHostAddress::Broadcast(255.255.255.255);
Multicast mode: also known as multicast. The UDP client joins another multicast group (qudpsocket:: joinmulticast group()) specified by the multicast IP address. All members in the group can receive the datagrams they want to send to the multicast address, which is similar to the function of QQ group. General instant messaging is based on UDP communication.
Code implementation:
udpsender: sender.h
#ifndef SENDER_H #define SENDER_H #include <QDialog> class QUdpSocket; namespace Ui { class Sender; } class Sender : public QDialog { Q_OBJECT public: explicit Sender(QWidget *parent = 0); ~Sender(); private slots: void on_pushButton_clicked();// Send datagram private: Ui::Sender *ui; QUdpSocket *sender; }; #endif // SENDER_H
udpsender: sender.cpp
#include "sender.h" #include "ui_sender.h" #include <QtNetwork> Sender::Sender(QWidget *parent) : QDialog(parent), ui(new Ui::Sender) { ui->setupUi(this); sender = new QUdpSocket(this); } Sender::~Sender() { delete ui; } void Sender::on_pushButton_clicked() { QByteArray datagram = "hello world!"; //Call the writeDatagram function to send data (both point-to-point and broadcast messages apply) sender->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 45454); }
udpreceiver: receiver.h
#ifndef RECEIVER_H #define RECEIVER_H #include <QDialog> class QUdpSocket; namespace Ui { class Receiver; } class Receiver : public QDialog { Q_OBJECT public: explicit Receiver(QWidget *parent = 0); ~Receiver(); private: Ui::Receiver *ui; QUdpSocket *receiver; private slots: void processPendingDatagram();// Read received datagrams }; #endif // RECEIVER_H
udpreceiver: receiver.cpp
#include "receiver.h" #include "ui_receiver.h" #include <QtNetwork> Receiver::Receiver(QWidget *parent) : QDialog(parent), ui(new Ui::Receiver) { ui->setupUi(this); receiver = new QUdpSocket(this); // To receive UDP data, you must first bind a port to listen for incoming datagrams and release the about function receiver->bind(45454, QUdpSocket::ShareAddress); // After receiving the datagram, QUdpSocket sends readyRead() signal to read the datagram of the buffer in the associated slot function processPendingDatagram connect(receiver, &QUdpSocket::readyRead, this, &Receiver::processPendingDatagram); } Receiver::~Receiver() { delete ui; } void Receiver::processPendingDatagram() { // Have waiting datagrams while(receiver->hasPendingDatagrams()) { QByteArray datagram; // Let the datagram size be the size of the datagram waiting to be processed, so that complete data can be received datagram.resize(receiver->pendingDatagramSize()); // Receive datagrams and store them in datagram receiver->readDatagram(datagram.data(), datagram.size()); ui->label->setText(datagram); } } //hasPendingDatagrams indicates whether incoming datagrams are to be read //pendingDatagramSize returns the number of bytes of the datagram to be read //readDatagram is used to read the contents of datagrams