当前位置:网站首页>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 ):
ends…
边栏推荐
- JPEG2000 matlab source code implementation
- Basic introduction of figure
- OpenCV300 CMake生成project在项目过程中的问题
- 1D convolution detail
- GPS从入门到放弃(十六)、卫星时钟误差和卫星星历误差
- GPS from getting started to giving up (XV), DCB differential code deviation
- Earned value management EVM detailed explanation and application, example explanation
- make menuconfig出现recipe for target ‘menuconfig‘ failed错误
- 基于LM317的可调直流电源
- Solution to the problem of UOS boot prompt unlocking login password ring
猜你喜欢

LeetCode:1189. The maximum number of "balloons" -- simple

Write a rotation verification code annotation gadget with aardio

Uni app app half screen continuous code scanning

GPS from entry to abandonment (XVII), tropospheric delay

AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing

【sciter】: 基于 sciter 封装通知栏组件

Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit

小满网络模型&http1-http2 &浏览器缓存
![Leetcode topic [array] -118 Yang Hui triangle](/img/77/d8a7085968cc443260b4c0910bd04b.jpg)
Leetcode topic [array] -118 Yang Hui triangle

Numpy download and installation
随机推荐
Kohana 数据库
npm run dev启动项目报错 document is not defined
Mongodb (III) - CRUD
Efficiency tool +wps check box shows the solution to the sun problem
bat脚本学习(一)
JPEG2000-Matlab源码实现
[Digital IC manual tearing code] Verilog automatic beverage machine | topic | principle | design | simulation
20 large visual screens that are highly praised by the boss, with source code templates!
基于LM317的可调直流电源
MySQL related terms
抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
Qt | UDP广播通信、简单使用案例
Run the deep network on PI and Jetson nano, and the program is killed
GPS from getting started to giving up (XV), DCB differential code deviation
数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
AI 企业多云存储架构实践 | 深势科技分享
Guava: three ways to create immutablexxx objects
hdu 4912 Paths on the tree(lca+馋)
make menuconfig出现recipe for target ‘menuconfig‘ failed错误
C language: comprehensive application of if, def and ifndef