当前位置:网站首页>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…
边栏推荐
- Search map website [quadratic] [for search map, search fan, search book]
- MariaDB database management system learning (I) installation diagram
- The underlying implementation of string
- C how to set two columns comboboxcolumn in DataGridView to bind a secondary linkage effect of cascading events
- [Chongqing Guangdong education] Information Literacy of Sichuan Normal University: a new engine for efficiency improvement and lifelong learning reference materials
- Problems in the process of opencv300 cmake generating project
- GPS从入门到放弃(十四)、电离层延时
- Depth first traversal (DFS) and breadth first traversal (BFS)
- Leetcode learning records (starting from the novice village, you can't kill out of the novice Village) ---1
- Five wars of Chinese Baijiu
猜你喜欢

Bat script learning (I)

C# 如何在dataGridView里设置两个列comboboxcolumn绑定级联事件的一个二级联动效果

PostgreSQL 修改数据库用户的密码

Method return value considerations

用aardio写一个旋转验证码标注小工具

Sequoia China, just raised $9billion

Checkpoint of RDD in spark

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

Adjustable DC power supply based on LM317

GPS从入门到放弃(十五)、DCB差分码偏差
随机推荐
[Yu Yue education] reference materials for surgical skills teaching in Tongji University
ViT论文详解
Unity3d Learning Notes 6 - GPU instantiation (1)
string的底层实现
bat脚本学习(一)
经纪xx系统节点VIP案例介绍和深入分析异常
Univariate cubic equation - relationship between root and coefficient
hdu 4912 Paths on the tree(lca+馋)
GPS from getting started to giving up (XVIII), multipath effect
From campus to Tencent work for a year of those stumbles!
基于InsightFace的高精度人脸识别,可直接对标虹软
What about the spectrogram
Uni app app half screen continuous code scanning
PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
Mongodb (III) - CRUD
关于程序员的职业操守,从《匠艺整洁之道》谈起
Bat script learning (I)
GPS from getting started to giving up (XV), DCB differential code deviation
numpy 下载安装
NPM run dev start project error document is not defined