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

QT 中 UDP 的使用

目录

一、UDP 简介

二、QT 中 UDP 编程的基本步骤

(一)包含头文件

(二)创建 UDP 套接字对象

(三)绑定端口

(四)发送数据

(五)接收数据

三、完整示例代码

(一)发送端代码

(二)接收端代码 

四、总结


一、UDP 简介

UDP(User Datagram Protocol,用户数据报协议)是一种无连接的传输层协议。与 TCP 相比,UDP 在数据传输时不需要建立连接,也不保证数据的可靠传输、顺序到达以及不重复。这使得 UDP 具有较低的开销和较高的传输效率,适用于对实时性要求较高,而对数据准确性要求相对较低的场景,如视频流、音频流传输等。

二、QT 中 UDP 编程的基本步骤

在 QT 框架下进行 UDP 编程,主要涉及以下几个关键步骤。

(一)包含头文件

首先,在源文件中需要包含QUdpSocket头文件,它提供了 UDP 套接字的功能实现。

#include <QUdpSocket>

(二)创建 UDP 套接字对象

在需要使用 UDP 的类中,声明一个QUdpSocket类型的成员变量。

class MyUdpClass : public QObject
{Q_OBJECT
public:MyUdpClass(QObject *parent = nullptr);
private:QUdpSocket *udpSocket;
};

在类的构造函数中,初始化这个 UDP 套接字对象。

MyUdpClass::MyUdpClass(QObject *parent) : QObject(parent)
{udpSocket = new QUdpSocket(this);
}

(三)绑定端口

为了能够接收和发送数据,需要将 UDP 套接字绑定到一个特定的端口上。可以使用bind函数进行绑定。

if (!udpSocket->bind(12345))
{qDebug() << "Failed to bind port";return;
}

这里尝试将 UDP 套接字绑定到端口 12345,如果绑定失败,会输出错误信息。

(四)发送数据

使用writeDatagram函数来发送 UDP 数据报。该函数需要指定发送的数据、目标主机的 IP 地址和端口号。

QByteArray data = "Hello, UDP!";
QHostAddress destAddress("192.168.1.100");
quint16 destPort = 54321;
qint64 bytesSent = udpSocket->writeDatagram(data, destAddress, destPort);
if (bytesSent == -1)
{qDebug() << "Failed to send data";
}

这段代码将字符串"Hello, UDP!"发送到目标 IP 地址为192.168.1.100,端口号为 54321 的主机上。如果发送失败,会输出相应的错误信息。 

(五)接收数据

为了接收数据,需要连接QUdpSocket的readyRead信号到一个槽函数,当有数据可读时,该槽函数会被调用。

connect(udpSocket, &QUdpSocket::readyRead, this, &MyUdpClass::readPendingDatagrams);

在槽函数readPendingDatagrams中,通过readDatagram函数读取数据。 

void MyUdpClass::readPendingDatagrams()
{while (udpSocket->hasPendingDatagrams()){QByteArray datagram;datagram.resize(udpSocket->pendingDatagramSize());QHostAddress sender;quint16 senderPort;udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);qDebug() << "Received datagram:" << datagram << "from" << sender.toString() << ":" << senderPort;}
}

这段代码会不断读取所有接收到的 UDP 数据报,并输出数据内容、发送方的 IP 地址和端口号。 

 

三、完整示例代码

下面是一个完整的 QT UDP 通信示例代码,包括发送端和接收端。

(一)发送端代码

#include <QCoreApplication>
#include <QUdpSocket>
#include <QDebug>int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QUdpSocket udpSocket;QByteArray data = "Hello, UDP from sender!";QHostAddress destAddress("192.168.1.100");quint16 destPort = 54321;qint64 bytesSent = udpSocket.writeDatagram(data, destAddress, destPort);if (bytesSent == -1){qDebug() << "Failed to send data";}else{qDebug() << "Data sent successfully";}return a.exec();
}

(二)接收端代码 

#include <QCoreApplication>
#include <QUdpSocket>
#include <QDebug>class UdpReceiver : public QObject
{Q_OBJECT
public:UdpReceiver(QObject *parent = nullptr);
private slots:void readPendingDatagrams();
private:QUdpSocket *udpSocket;
};UdpReceiver::UdpReceiver(QObject *parent) : QObject(parent)
{udpSocket = new QUdpSocket(this);if (!udpSocket->bind(54321)){qDebug() << "Failed to bind port";return;}connect(udpSocket, &QUdpSocket::readyRead, this, &UdpReceiver::readPendingDatagrams);
}void UdpReceiver::readPendingDatagrams()
{while (udpSocket->hasPendingDatagrams()){QByteArray datagram;datagram.resize(udpSocket->pendingDatagramSize());QHostAddress sender;quint16 senderPort;udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);qDebug() << "Received datagram:" << datagram << "from" << sender.toString() << ":" << senderPort;}
}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);UdpReceiver receiver;return a.exec();
}

四、总结

通过以上步骤和示例代码,我们可以在 QT 中实现基本的 UDP 通信功能。在实际应用中,还需要根据具体需求对代码进行优化和扩展,例如处理网络异常、实现更复杂的数据结构传输等。UDP 在实时性要求高的场景中有着广泛的应用,掌握 QT 中 UDP 的编程方法,有助于开发出高效的网络应用程序。

 

 

 

 

 

 

 

 

 

http://www.lryc.cn/news/524384.html

相关文章:

  • leetcode刷题记录(七十二)——146. LRU 缓存
  • 深圳大学-计算机系统(3)-实验一MIPS指令集实验
  • Java面试专题——面向对象
  • 知行合一:解决有心无力的问题,解决知易行难的问题,知行合一并不意味着事事都要合一,而是....
  • Qt中自定义信号与槽
  • .NET 8 项目 Docker 方式部署到 Linux 系统详细操作步骤
  • 深入了解 Java split() 方法:分割字符串的利器
  • pgsql中处理数组类型字段
  • 如何正确定位前后端bug?
  • mfc操作json示例
  • 【技术总结类】2024,一场关于海量数据治理以及合理建模的系列写作
  • Dockerfile另一种使用普通用户启动的方式
  • python的pushbullet库在设备之间发送通知链接文件
  • STM32之FreeRTOS开发介绍(十九)
  • 用java配合redis 在springboot上实现令牌桶算法
  • DCGAN - 深度卷积生成对抗网络:基于卷积神经网络的GAN
  • 51c~SLAM~合集1
  • 优化使用 Flask 构建视频转 GIF 工具
  • spring cloud如何实现负载均衡
  • leetcode19-删除链表的第n结点
  • 软件测试—— 接口测试(HTTP和HTTPS)
  • 3.1 Go函数调用过程
  • TDengine 做 Apache SuperSet 数据源
  • 08_游戏启动逻辑
  • Ardupilot开源无人机之Geek SDK进展2024-2025
  • 在K8S中,如果后端NFS存储的IP发送变化如何解决?
  • 模拟飞行入坑(五) P3D 多通道视角配置 viewgroup
  • 【springboot集成knife4j】
  • GPUStack使用
  • 如何选择一款助贷获客系统?