当前位置:网站首页>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…
边栏推荐
- 十一、服务介绍及端口
- Make menuconfig has a recipe for target 'menuconfig' failed error
- Save and retrieve strings
- Basic introduction of figure
- GNN,请你的网络层数再深一点~
- 2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
- [Chongqing Guangdong education] Tianjin urban construction university concrete structure design principle a reference
- HDU 2008 数字统计
- 保存和检索字符串
- npm run dev启动项目报错 document is not defined
猜你喜欢
Checkpoint of RDD in spark
GNN,请你的网络层数再深一点~
Unity3D学习笔记6——GPU实例化(1)
Michael smashed the minority milk sign
PostgreSQL install GIS plug-in create extension PostGIS_ topology
Huawei has launched attacks in many industries at the same time, and its frightening technology has made European and American enterprises tremble
GPS from getting started to giving up (XV), DCB differential code deviation
Reptile practice (V): climbing watercress top250
Leetcode learning records (starting from the novice village, you can't kill out of the novice Village) ---1
GPS从入门到放弃(十七) 、对流层延时
随机推荐
MySQL related terms
GPS从入门到放弃(十七) 、对流层延时
The role of applicationmaster in spark on Yan's cluster mode
[Yu Yue education] reference materials for surgical skills teaching in Tongji University
Tiktok will push the independent grass planting app "praiseworthy". Can't bytes forget the little red book?
JPEG2000 matlab source code implementation
基于LM317的可调直流电源
Explain ESM module and commonjs module in simple terms
Enhance network security of kubernetes with cilium
GPS从入门到放弃(十一)、差分GPS
Realization of epoll reactor model
Basic introduction of figure
Method return value considerations
PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
[asp.net core] set the format of Web API response data -- formatfilter feature
1292_ Implementation analysis of vtask resume() and xtask resume fromisr() in freeros
LeetCode学习记录(从新手村出发之杀不出新手村)----1
GPS从入门到放弃(十五)、DCB差分码偏差
Why is the cluster mode of spark on Yan better than the client mode
bat脚本学习(一)