当前位置: 首页 > news >正文

Qt UDP 通信的详细实现步骤和示例代码

在 Qt 中实现 UDP 通信主要使用 QUdpSocket 类。以下是 UDP 通信的详细实现步骤和示例代码:


一、UDP 通信基础

  • 无连接协议:不需要建立持久连接
  • 数据报模式:以独立数据包(datagram)形式发送
  • 适用场景:实时性要求高、允许少量丢包(如视频流、游戏、传感器数据)

二、核心类 QUdpSocket

1. 发送数据流程
// 创建 socket 对象
QUdpSocket *udpSocket = new QUdpSocket(this);// 准备数据
QByteArray datagram = "Hello UDP!";
QHostAddress targetAddress = QHostAddress("192.168.1.100");
quint16 targetPort = 1234;// 发送数据报(单播)
qint64 bytesSent = udpSocket->writeDatagram(datagram, targetAddress, targetPort);// 广播示例(发送到所有子网设备)
// udpSocket->writeDatagram(datagram, QHostAddress::Broadcast, 1234);
2. 接收数据流程
// 创建 socket 并绑定端口
QUdpSocket *udpSocket = new QUdpSocket(this);
bool bindResult = udpSocket->bind(1234);  // 绑定接收端口// 连接信号槽
connect(udpSocket, &QUdpSocket::readyRead, [=]() {while (udpSocket->hasPendingDatagrams()) {QByteArray datagram;datagram.resize(udpSocket->pendingDatagramSize());QHostAddress senderAddress;quint16 senderPort;udpSocket->readDatagram(datagram.data(), datagram.size(),&senderAddress, &senderPort);qDebug() << "Received from" << senderAddress.toString() << ":" << senderPort<< "Data:" << datagram;}
});// 错误处理
connect(udpSocket, &QUdpSocket::errorOccurred, [=](QAbstractSocket::SocketError error){qDebug() << "UDP Error:" << udpSocket->errorString();
});

三、多播(组播)实现

// 加入多播组
QHostAddress multicastGroup("239.255.43.21");
udpSocket->joinMulticastGroup(multicastGroup);// 设置TTL(生存时间)
udpSocket->setSocketOption(QAbstractSocket::MulticastTtlOption, 1);// 发送多播数据
udpSocket->writeDatagram(datagram, multicastGroup, 1234);// 离开多播组
udpSocket->leaveMulticastGroup(multicastGroup);

四、完整示例

发送端类
class UdpSender : public QObject {
public:UdpSender(QObject *parent = nullptr) : QObject(parent) {socket = new QUdpSocket(this);}void sendMessage(const QString &message, const QHostAddress &target, quint16 port) {QByteArray data = message.toUtf8();socket->writeDatagram(data, target, port);}private:QUdpSocket *socket;
};
接收端类
class UdpReceiver : public QObject {Q_OBJECT
public:UdpReceiver(quint16 port, QObject *parent = nullptr) : QObject(parent) {socket = new QUdpSocket(this);connect(socket, &QUdpSocket::readyRead, this, &UdpReceiver::processPendingDatagrams);socket->bind(port);}private slots:void processPendingDatagrams() {while (socket->hasPendingDatagrams()) {QByteArray datagram;datagram.resize(socket->pendingDatagramSize());QHostAddress sender;quint16 senderPort;socket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);emit messageReceived(QString::fromUtf8(datagram), sender.toString(), senderPort);}}signals:void messageReceived(const QString &message, const QString &senderIp, quint16 senderPort);private:QUdpSocket *socket;
};

五、注意事项

  1. .pro 文件中添加网络模块:

    QT += network
    
  2. UDP 数据报最大长度通常为 65507 字节(IPv4)

  3. 处理数据时考虑:

    • 数据可能分片到达
    • 可能收到不完整数据包
    • 需要自行处理数据校验
  4. 广播地址:

    • 受限广播:255.255.255.255
    • 子网广播:192.168.1.255(具体取决于子网掩码)

通过以上实现,您可以轻松在 Qt 应用中完成 UDP 通信功能。根据具体需求选择单播、广播或多播模式,并注意处理可能的网络异常情况。

http://www.xdnf.cn/news/29359.html

相关文章:

  • spring boot应用部署IIS
  • matlab论文图一的地形区域图的球形展示Version_1
  • 基于springboot的老年医疗保健系统
  • 【Matlab】中国东海阴影立体感地图
  • 【蓝桥杯 2025 省 A 扫地机器人】题解
  • Graham Scan算法求解二维凸包
  • 通过Xshell上传文件到Linux
  • Python:使用web框架Flask搭建网站
  • JS案例-Promise/A+ 规范的手写实现
  • 【厦门大学】DeepSeek大模型赋能政府数字化转型
  • OSPF实验
  • React-memo (useMemo, useCallback)
  • PG数据库推进医疗AI向量搜索优化路径研究(2025年3月修订版)
  • 破解保险箱
  • WinForms开发基础:实现带X按钮的ClearableTextBox控件
  • spring-batch批处理框架(2)
  • EAGLE代码研读+模型复现
  • Windows使用SonarQube时启动脚本自动关闭
  • 运算符重载
  • 小刚说C语言刷题——1035 判断成绩等级
  • firewalld 防火墙
  • 深入实战:使用C++开发高性能RESTful API
  • 详解反射型 XSS 的后续利用方式:从基础窃取到高级组合拳攻击链
  • 基于Python的中国象棋小游戏的设计与实现
  • CiteULike 数据集介绍与下载指南
  • AI时代下 你需要和想要了解的英文缩写含义
  • 基于单片机的热释电红外报警器(论文+源码)
  • 基于单片机的按摩器控制系统设计
  • 单例设计模式
  • springCloud/Alibaba常用中间件全集(上)