当前位置:网站首页>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…
边栏推荐
- C language char, wchar_ t, char16_ t, char32_ Relationship between T and character set
- The underlying implementation of string
- OpenCV300 CMake生成project在项目过程中的问题
- Qt | UDP广播通信、简单使用案例
- Save and retrieve strings
- 14 years Bachelor degree, transferred to software testing, salary 13.5k
- Leetcode learning records (starting from the novice village, you can't kill out of the novice Village) ---1
- PostgreSQL modifies the password of the database user
- Reinforcement learning - learning notes 5 | alphago
- 数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
猜你喜欢
Write a rotation verification code annotation gadget with aardio
Leetcode learning records (starting from the novice village, you can't kill out of the novice Village) ---1
uni-app App端半屏连续扫码
Happy sound 2[sing.2]
Basic introduction of figure
PostgreSQL modifies the password of the database user
make menuconfig出现recipe for target ‘menuconfig‘ failed错误
Michael smashed the minority milk sign
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
基于LM317的可调直流电源
随机推荐
[Yu Yue education] higher mathematics of Nanchang University (2) reference materials
【sciter】: 基于 sciter 封装通知栏组件
Reset Mikrotik Routeros using netinstall
Guava: use of multiset
GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
华为在多个行业同时出击,吓人的技术让欧美企业瑟瑟发抖
MySQL - transaction details
Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit
Broadcast variables and accumulators in spark
Earned value management EVM detailed explanation and application, example explanation
Problems in the process of opencv300 cmake generating project
Run the deep network on PI and Jetson nano, and the program is killed
在Pi和Jetson nano上运行深度网络,程序被Killed
The golden age of the U.S. technology industry has ended, and there have been constant lamentations about chip sales and 30000 layoffs
JPEG2000-Matlab源码实现
Sparkshuffle process and Mr shuffle process
Powerful domestic API management tool
Vit paper details
uni-app App端半屏连续扫码
guava:Collections. The collection created by unmodifiablexxx is not immutable