当前位置:网站首页>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…
边栏推荐
- GNN,请你的网络层数再深一点~
- Reinforcement learning - learning notes 5 | alphago
- OpenCV300 CMake生成project在项目过程中的问题
- Solution to the problem of UOS boot prompt unlocking login password ring
- Run the deep network on PI and Jetson nano, and the program is killed
- Set status bar style demo
- Persistence / caching of RDD in spark
- MPLS experiment
- [Yu Yue education] higher mathematics of Nanchang University (2) reference materials
- 抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
猜你喜欢
GNN,请你的网络层数再深一点~
嵌入式常用计算神器EXCEL,欢迎各位推荐技巧,以保持文档持续更新,为其他人提供便利
Unity3D学习笔记6——GPU实例化(1)
Huawei has launched attacks in many industries at the same time, and its frightening technology has made European and American enterprises tremble
Broadcast variables and accumulators in spark
GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)
GPS从入门到放弃(十五)、DCB差分码偏差
JPEG2000-Matlab源码实现
PostgreSQL install GIS plug-in create extension PostGIS_ topology
PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
随机推荐
数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
Adjustable DC power supply based on LM317
关于char[]数组通过scanf赋值使用上的一些问题。。
关于程序员的职业操守,从《匠艺整洁之道》谈起
GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
guava:Collections.unmodifiableXXX创建的collection并不immutable
What can one line of code do?
guava:创建immutableXxx对象的3种方式
在Pi和Jetson nano上运行深度网络,程序被Killed
Yyds dry goods inventory C language recursive implementation of Hanoi Tower
OpenCV300 CMake生成project在项目过程中的问题
Oracle性能分析3:TKPROF简介
guava: Multiset的使用
Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
JPEG2000 matlab source code implementation
Redistemplate common collection instructions opsforlist (III)
Reinforcement learning - learning notes 5 | alphago
Record the process of cleaning up mining viruses
Basic introduction of figure