当前位置:网站首页>Qt | UDP广播通信、简单使用案例
Qt | UDP广播通信、简单使用案例
2022-07-06 13:48:00 【雍正不秃头】
Qt | UDP广播通信、简单使用案例
1、UDP广播介绍
UDP广播地址固定IP地址为:255.255.255.255,
接收方绑定0.0.0.0地址并监听指定端口即可收到广播的群发消息。
2、使用场景
某设备的业务通信是使用TCP协议,当该设备部署到一个新的网络环境当中时,设备的IP地址是未知的,需要知道或修改设备的IP地址才能够进行TCP的通信。
此时就可以在局域网内通过UDP广播的形式使该设备收到广播消息,从而使用设备返回IP地址、返回设备的MAC、修改IP地址等操作。
3、Qt UDP广播示例
服务器(发送广播消息端)示例代码:
我这里设定的端口为10123,不是固定的。但是服务器往哪个端口发送广播消息,客户端就得监听哪个端口。
#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); // 将客户端发来的数据在发送回去
}
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
socket->writeDatagram("this is broadcast message !", QHostAddress::Broadcast, 10123); // 发送广播消息
}
客户端(接收广播消息端)示例代码:
#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、运行效果
一个服务端用于发布广播消息,两个客户端(网络调试助手是客户端):
ends…
边栏推荐
- Depth first traversal (DFS) and breadth first traversal (BFS)
- GPS从入门到放弃(十六)、卫星时钟误差和卫星星历误差
- GPS从入门到放弃(十二)、 多普勒定速
- MySQL removes duplicates according to two fields
- JPEG2000-Matlab源码实现
- Intelligent online customer service system source code Gofly development log - 2 Develop command line applications
- Numpy download and installation
- GPS从入门到放弃(十九)、精密星历(sp3格式)
- [Yu Yue education] higher mathematics of Nanchang University (2) reference materials
- 1292_FreeROS中vTaskResume()以及xTaskResumeFromISR()的实现分析
猜你喜欢

Enhance network security of kubernetes with cilium

GPS从入门到放弃(十五)、DCB差分码偏差

GPS from entry to abandonment (XIV), ionospheric delay

C how to set two columns comboboxcolumn in DataGridView to bind a secondary linkage effect of cascading events

20 large visual screens that are highly praised by the boss, with source code templates!

PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology

Tiktok will push the independent grass planting app "praiseworthy". Can't bytes forget the little red book?

Huawei has launched attacks in many industries at the same time, and its frightening technology has made European and American enterprises tremble

小满网络模型&http1-http2 &浏览器缓存

GPS从入门到放弃(十二)、 多普勒定速
随机推荐
14 years Bachelor degree, transferred to software testing, salary 13.5k
Redistemplate common collection instructions opsforhash (IV)
[Li Kou brush questions] 32 Longest valid bracket
guava: Multiset的使用
Run the deep network on PI and Jetson nano, and the program is killed
MySQL - transaction details
Leetcode topic [array] -118 Yang Hui triangle
GPS from getting started to giving up (XX), antenna offset
GPS從入門到放弃(十三)、接收機自主完好性監測(RAIM)
红杉中国,刚刚募资90亿美元
[Yu Yue education] higher mathematics of Nanchang University (2) reference materials
记一次清理挖矿病毒的过程
JS method to stop foreach
50个常用的Numpy函数解释,参数和使用示例
搜素专题(DFS )
GPS从入门到放弃(十七) 、对流层延时
Technology sharing | packet capturing analysis TCP protocol
Search map website [quadratic] [for search map, search fan, search book]
uni-app App端半屏连续扫码
GPS从入门到放弃(十九)、精密星历(sp3格式)