当前位置:网站首页>QT | UDP broadcast communication, simple use case

QT | UDP broadcast communication, simple use case

2022-07-06 22:02:00 Yongzheng not bald

Qt | UDP Broadcast communications 、 Simple use cases


1、UDP Broadcast Introduction

UDP The broadcast address is fixed IP The address is :255.255.255.255,

Receiver binding 0.0.0.0 Address and listen to the specified port to receive the broadcast mass message .

2、 Use scenarios

The business communication of a device uses TCP agreement , When the device is deployed to a new network environment , The equipment IP The address is unknown , Need to know or modify the equipment IP Address can be TCP Communication for .

At this time, you can pass through the LAN UDP The form of broadcast enables the device to receive broadcast messages , Thus using the device to return IP Address 、 Returns the name of the device MAC、 modify IP Address and other operations .

3、Qt UDP Broadcast example

The server ( Send broadcast message end ) Sample code :

The port I set here is 10123, Not fixed . But which port does the server send broadcast messages to , The client has to listen on which port .

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    
    ui->setupUi(this);

    socket = new QUdpSocket(this);
    connect(socket, &QUdpSocket::readyRead, this, [=](){
    
        while(socket->hasPendingDatagrams())
        {
    
            QByteArray data;
            data.resize(socket->pendingDatagramSize());
            QHostAddress host;
            quint16 port;
            socket->readDatagram(data.data(), data.size(), &host, &port);  //  Send the data sent by the client back 
        }
    });
}

MainWindow::~MainWindow()
{
    
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    
    socket->writeDatagram("this is broadcast message !", QHostAddress::Broadcast, 10123);  //  Send broadcast messages 
}

client ( Receive broadcast message end ) Sample code :

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    
    ui->setupUi(this);

    socket = new QUdpSocket(this);
    if(!socket->bind(10123, QUdpSocket::ShareAddress))
    {
    
        ui->textBrowser->append("bind failed !");
    }

    connect(socket, &QUdpSocket::readyRead, this, [=](){
    
        while(socket->hasPendingDatagrams())
        {
    
            QByteArray data;
            data.resize(socket->pendingDatagramSize());
            QHostAddress host;
            quint16 port;
            socket->readDatagram(data.data(), data.size(), &host, &port);

            QString text = QString("[%1:%2]:%3").arg(host.toString()).arg(port).arg(QString(data));

            ui->textBrowser->append(text);
        }
    });
}

MainWindow::~MainWindow()
{
    
    delete ui;
}

4、 Running effect

A server is used to publish broadcast messages , Two clients ( The network debugging assistant is the client ):
 Insert picture description here


ends…

原网站

版权声明
本文为[Yongzheng not bald]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061347420985.html