当前位置:网站首页>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…
边栏推荐
猜你喜欢

GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)

Unity3D学习笔记6——GPU实例化(1)

Aggregate function with key in spark

GPS from entry to abandonment (XIV), ionospheric delay

功能强大的国产Api管理工具
![[Li Kou brush questions] 32 Longest valid bracket](/img/51/1ce4f9e8517dba214ec82b6567c923.png)
[Li Kou brush questions] 32 Longest valid bracket

Make menuconfig has a recipe for target 'menuconfig' failed error

Enhance network security of kubernetes with cilium

The golden age of the U.S. technology industry has ended, and there have been constant lamentations about chip sales and 30000 layoffs

GPS从入门到放弃(十五)、DCB差分码偏差
随机推荐
Fzu 1686 dragon mystery repeated coverage
GPS从入门到放弃(二十)、天线偏移
ROS error: could not find a package configuration file provided by "move_base“
GPS从入门到放弃(十九)、精密星历(sp3格式)
[Li Kou brushing questions] one dimensional dynamic planning record (53 change exchanges, 300 longest increasing subsequence, 53 largest subarray and)
爬虫实战(五):爬豆瓣top250
Search map website [quadratic] [for search map, search fan, search book]
【sciter】: 基于 sciter 封装通知栏组件
用aardio写一个旋转验证码标注小工具
PostgreSQL 修改数据库用户的密码
Unity3D学习笔记6——GPU实例化(1)
guava:创建immutableXxx对象的3种方式
Guava: three ways to create immutablexxx objects
[Li Kou brush questions] 32 Longest valid bracket
Uni app app half screen continuous code scanning
功能强大的国产Api管理工具
中国白酒的5场大战
Leetcode topic [array] -118 Yang Hui triangle
Basic introduction of figure
Some problems about the use of char[] array assignment through scanf..