Qt中对Udp数据打包发送和接收

有些小伙伴对怎么对Udp的数据打包不太清楚。下面我举例说明。

比如我们要发送一个Person的数据。可以先用一个结构把Person的数据封装。

struct Person {
    QString name;
    int age;
};

下面是udp客户端和服务器端完整的代码例子。


#ifndef UDPCLIENT_H
#define UDPCLIENT_H

#include 
#include 

struct Person {
    QString name;
    int age;
};

class UdpClient : public QObject
{
    Q_OBJECT
public:


    explicit UdpClient(QObject *parent = nullptr);
    void sendDatagram(const QByteArray &data);

private slots:
    void readPendingDatagrams();

private:
    void processDatagram(const QNetworkDatagram &datagram);
    QUdpSocket *udpSocket;
};

#endif // UDPCLIENT_H
        

#include "UdpClient.h"
#include 
#include 

UdpClient::UdpClient(QObject *parent) : QObject(parent) {
    udpSocket = new QUdpSocket(this);
    udpSocket->bind(QHostAddress::LocalHost, 1235);

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

void UdpClient::sendDatagram(const QByteArray &data) {
    udpSocket->writeDatagram(data, QHostAddress::LocalHost, 1234);
}

void UdpClient::readPendingDatagrams() {
    while (udpSocket->hasPendingDatagrams()) {
        QNetworkDatagram datagram = udpSocket->receiveDatagram();
        processDatagram(datagram);
    }
}

void UdpClient::processDatagram(const QNetworkDatagram &datagram) {
    QByteArray data = datagram.data();
    // Here you would process the data
    QDataStream stream(data);
   Person person;
   stream >> person.name >> person.age;

   // 现在你可以使用 person 结构中的数据
   qDebug() << "Received person: " << person.name << ", " << person.age;
}
        

#ifndef UDPSERVER_H
#define UDPSERVER_H

#include 
#include 

class UdpServer : public QObject
{
    Q_OBJECT
public:
    explicit UdpServer(QObject *parent = nullptr);

private slots:
    void readPendingDatagrams();

private:
    void processDatagram(const QNetworkDatagram &datagram);
    QUdpSocket *udpSocket;
};

#endif // UDPSERVER_H

#include "UdpServer.h"

#include 

UdpServer::UdpServer(QObject *parent) : QObject(parent) {
    udpSocket = new QUdpSocket(this);
    udpSocket->bind(QHostAddress::LocalHost, 1234);

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

void UdpServer::readPendingDatagrams() {
    while (udpSocket->hasPendingDatagrams()) {
        QNetworkDatagram datagram = udpSocket->receiveDatagram();
        processDatagram(datagram);
    }
}

void UdpServer::processDatagram(const QNetworkDatagram &datagram) {
    QByteArray data = datagram.data();
    // Here you would process the data
    // ...
    //qDebug() << "Received data from client: " << data.toStdString().c_str();

    // Sending a response back to the sender
    udpSocket->writeDatagram(data, datagram.senderAddress(), datagram.senderPort());
}
        

#include 
#include 
#include "UdpServer.h"
#include "UdpClient.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    UdpServer server;
    UdpClient client;

    Person person;
    person.name = "Alice";
    person.age = 30;

    QByteArray datagram;
    QDataStream stream(&datagram, QIODevice::WriteOnly);
    stream << person.name << person.age;

    client.sendDatagram(datagram);

    return a.exec();
}
        
        

你可能感兴趣的:(Qt,udp,网络协议,网络)