> 文档中心 > Qt之超简单的UDP通信(自定义UDP通信类,含源码+注释)

Qt之超简单的UDP通信(自定义UDP通信类,含源码+注释)


一、UDP通信示例图

下图为UDP通信的简单界面,能是实现绑定本地IP、端口号和指定目标IP和端口号以及接收、发送数据功能,源码在本文第四节(源码含详细注释)
在这里插入图片描述

二、UDP使用前的准备

  1. 在pro文件中添加 “QT += network”(需要添加后才能使用网络通信)
  2. UDP发送可以通过writeDatagram函数(需要指定目标ip和端口号)
  3. UDP读取可以通过readDatagram函数(需要接收数据的char *、接收数据的长度以及能保存目标ip和端口号的变量指针)
  4. UDP收到新信息时会发出readyRead信号
  5. UDP通信需要保存目标端的IP和端口号(方便通信)

三、自定义UDP通信类的两种方法

  1. 类继承对应的UDP类,通过this指针实现UDP通信操作
  2. 类中包含对应的UDP类对象,通过该对象实现UDP通信操作(本文使用该方法)

四、源码(含注释)

自定义UDP类

CUdpSocket.h

#ifndef CUDPSOCKET_H#define CUDPSOCKET_H#include #include #include class CUdpSocket : public QObject{    Q_OBJECTpublic:    explicit CUdpSocket(QObject *parent = nullptr);    ~CUdpSocket();    //绑定本机的ip和端口号信息    void bind(QString ip, int port);    //通过该函数发送数据    void sendData(QString data);    //设置目标主机的ip和端口号    void setTargetInfo(QString ip, int port);    signals:    //通过该信号传递接收到的数据    void recvDataSignal(QString data);public slots:    //读取数据的槽函数    void readyReadData();    private:    QUdpSocket      *m_sock; //udp套接字指针    QHostAddress    m_hostAddr;     //保存目标的地址对象    quint16  m_port;  //保存目标的端口号(类型一致)};#endif // CUDPSOCKET_H

CUdpSocket.cpp

#include "CUdpSocket.h"CUdpSocket::CUdpSocket(QObject *parent) : QObject(parent){    m_sock = new QUdpSocket;    connect(m_sock, &QUdpSocket::readyRead, this, &CUdpSocket::readyReadData);}CUdpSocket::~CUdpSocket(){    delete m_sock;}void CUdpSocket::bind(QString ip, int port){    m_sock->bind(QHostAddress(ip), port);}void CUdpSocket::sendData(QString data){    m_sock->writeDatagram(data.toUtf8(), m_hostAddr, m_port);}void CUdpSocket::setTargetInfo(QString ip, int port){    m_hostAddr = QHostAddress(ip);    m_port = port;}void CUdpSocket::readyReadData(){    while(m_sock->hasPendingDatagrams())    { //创建数据存储容器,并设置长度为将接收的数据长度 QByteArray data; data.resize(m_sock->pendingDatagramSize()); //读取数据并保存信息发送者的地址和ip(方便发送时指定发送位置) m_sock->readDatagram(data.data(), data.size(), &m_hostAddr, &m_port); emit recvDataSignal(data);    }}

CMainWindow类(自定义TCP通信类的调用)

CMainWindow.h

#ifndef CMAINWINDOW_H#define CMAINWINDOW_H#include #include "CUdpSocket.h"namespace Ui {class CMainWindow;}class CMainWindow : public QMainWindow{    Q_OBJECTpublic:    explicit CMainWindow(QWidget *parent = 0);    ~CMainWindow();private slots:    void on_bindBtn_clicked();//绑定    void on_sendBtn_clicked();//发送    void on_appendData(QString data);//将接收的数据添加显示private:    Ui::CMainWindow *ui;    CUdpSocket *m_udpSock;//UDP自定义类的指针};#endif // CMAINWINDOW_H

CMainWindow.cpp

#include "CMainWindow.h"#include "ui_CMainWindow.h"CMainWindow::CMainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::CMainWindow){    ui->setupUi(this);    m_udpSock = new CUdpSocket;    connect(m_udpSock, &CUdpSocket::recvDataSignal, this, &CMainWindow::on_appendData);}CMainWindow::~CMainWindow(){    delete m_udpSock;    delete ui;}void CMainWindow::on_bindBtn_clicked(){    if("已绑定" == ui->bindBtn->text()) return;    ui->bindBtn->setText("已绑定");    m_udpSock->bind(ui->ipEdit->text(), ui->portEdit->text().toInt());    m_udpSock->setTargetInfo(ui->ipEdit_2->text(), ui->portEdit_2->text().toInt());}void CMainWindow::on_sendBtn_clicked(){    m_udpSock->sendData(ui->textEdit->toPlainText());}void CMainWindow::on_appendData(QString data){    ui->textBrowser->append(data);}

总结

UDP需要绑定本地IP和端口号,并且需要指定目标IP和端口号才能发送消息,并且在发送信息和接收信息的时候需要接收和指定目标主机的信息(不过本文只是简单的UDP通信功能,有兴趣可以补全UDP的其他功能)

相关文档

自定义TCP类

友情提示——哪里看不懂可私哦,让我们一起互相进步吧
(创作不易,请留下一个免费的赞叭 谢谢 ^o^/)

注:文章为作者编程过程中所遇到的问题和总结,内容仅供参考,若有错误欢迎指出。
注:如有侵权,请联系作者删除

英文字体下载