QT中,对于大小端UDP网络发送的demo,帧头帧尾

简单demo:

发送端:

cpp 复制代码
#include <QUdpSocket>
#include <QtEndian>

#pragma pack(1)
struct Test
{
    unsigned char   t1:1;
    unsigned char   t2:2;
    unsigned char   t3:3;
    unsigned char   t4:2;
    quint8    a = 1;
    quint16   b = 2;
    quint16   c = 3;
    //double    b = 2;
    quint8    d = 4;
    quint8    e = 1;
};
#pragma pack(pop)

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QByteArray temp_byte_array;
    Test test;
    test.b = qToBigEndian(test.b);
    test.c = qToBigEndian(test.c);
    test.b = qToBigEndian(test.b);
    test.t1 = 0x1;
    test.t2 = 0x2;
    test.t3 = 0x3;
    test.t4 = 0x2;

    QUdpSocket *m_pUdpSocket = new QUdpSocket();
    m_pUdpSocket->bind(QHostAddress("127.0.0.1"),8002);
    QByteArray frame = QByteArray((char *)&test, sizeof(Test));
    m_pUdpSocket->writeDatagram(frame, QHostAddress("127.0.0.1"), 8001);

    return 0;
}

接收:

cpp 复制代码
#include <QUdpSocket>
#include <QtEndian>

#pragma pack(1)
struct Test
{
    unsigned char   t1:1;
    unsigned char   t2:2;
    unsigned char   t3:3;
    unsigned char   t4:2;
    quint8    a = 0x1;
    quint16   b = 0x2;
    quint16   c = 0x3;
    //double    b = 2;
    quint8    d = 0x4;
    quint8    e = 0x1;
};
#pragma pack(pop)

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

    QApplication app(argc, argv);
    QUdpSocket *m_pUdpSocket = new QUdpSocket();
    m_pUdpSocket->bind(QHostAddress("127.0.0.1"),8001);
    QObject::connect(m_pUdpSocket,&QUdpSocket::readyRead,[=](){
        QByteArray frame;
        frame.resize(m_pUdpSocket->pendingDatagramSize());
        // 接收数据报,将其存放到datagram中
        m_pUdpSocket->readDatagram(frame.data(), frame.size());
        struct Test *pFrame = (struct Test *)frame.data();
        qDebug()<<"a:"<<pFrame->a;
        qDebug()<<"b:"<<pFrame->b;
        qDebug()<<"c:"<<pFrame->c;
        qDebug()<<"d:"<<pFrame->d;
        qDebug()<<"*****************";
    });

    return app.exec();
}

有帧头帧尾:

发送:

cpp 复制代码
#include <QUdpSocket>
#include <QtEndian>

// 以1字节对齐:
#pragma pack(1)
struct Test
{
    // 帧头 0xabcd
    unsigned short dataHead = 0xabcd;

    // 小于1字节:
    unsigned char   t1:1;
    unsigned char   t2:2;
    unsigned char   t3:3;
    unsigned char   t4:2;

    // 小于或者大于1字节
    quint8    a = 0x1;
    quint16   b = 0x2;
    quint16   c = 0x3;
    quint8    d = 0x4;
    quint8    e = 0x1;

    // 包尾 dcba
    unsigned short dataTail = 0xdcba;
};
#pragma pack(pop)

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

    QByteArray temp_byte_array;
    Test test;
    //test.b = qToBigEndian(test.b);
    //test.c = qToBigEndian(test.c);
    test.t1 = 0x1;
    test.t2 = 0x2;
    test.t3 = 0x3;
    test.t4 = 0x2;

    QUdpSocket *m_pUdpSocket = new QUdpSocket();
    m_pUdpSocket->bind(QHostAddress("127.0.0.1"),8002);
    QByteArray frame = QByteArray((char *)&test, sizeof(Test));
    m_pUdpSocket->writeDatagram(frame, QHostAddress("127.0.0.1"), 8001);

}

接受:

cpp 复制代码
#include <QUdpSocket>
#include <QtEndian>

// 以1字节对齐:
#pragma pack(1)
struct Test
{
    // 帧头 0xabcd
    unsigned short dataHead = 0xabcd;

    // 小于1字节:
    unsigned char   t1:1;
    unsigned char   t2:2;
    unsigned char   t3:3;
    unsigned char   t4:2;

    // 小于或者大于1字节
    quint8    a = 0x1;
    quint16   b = 0x2;
    quint16   c = 0x3;
    quint8    d = 0x4;
    quint8    e = 0x1;

    // 包尾 dcba
    unsigned short dataTail = 0xdcba;
};
#pragma pack(pop)

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

    QApplication app(argc, argv);
    QUdpSocket *m_pUdpSocket = new QUdpSocket();
    m_pUdpSocket->bind(QHostAddress("127.0.0.1"),8001);
    QObject::connect(m_pUdpSocket,&QUdpSocket::readyRead,[=](){
        QByteArray frame;
        frame.resize(m_pUdpSocket->pendingDatagramSize());
        // 接收数据报,将其存放到datagram中
        m_pUdpSocket->readDatagram(frame.data(), frame.size());
        struct Test *pFrame = (struct Test *)frame.data();
        if(pFrame->dataHead == 0xabcd && pFrame->dataTail == 0xdcba)
        {
            qDebug()<<"接收到信息*********";
            qDebug()<<"a:"<<pFrame->a;
            qDebug()<<"b:"<<pFrame->b;
            qDebug()<<"c:"<<pFrame->c;
            qDebug()<<"d:"<<pFrame->d;
            qDebug()<<"*****************";
        }

    });

    return app.exec();
}

会发现,如果不是这个帧头和帧尾,就会出问题,只有帧头帧尾正确,才可以正常解析。

注意事项:

  1. 结构体前后一定要加1字节对齐,否则可能会多出很多字节,比如你们可以自己尝试不加一字节对齐,然后加入double会增加多少的字节。

  2. 接收的时候,不要使用readall() 。

  3. 测试的时候,发送不要使用串口调试助手等工具,哪些会出问题。

  4. 大小端:很多系统默认小端,如果需要转为大端发送数据,大于1字节的数据,需要使用qToBigEndian()来转换。

相关推荐
计算机学长felix几秒前
基于SpringBoot的“校园交友网站”的设计与实现(源码+数据库+文档+PPT)
数据库·spring boot·毕业设计·交友
Re.不晚3 分钟前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
老秦包你会6 分钟前
Qt第三课 ----------容器类控件
开发语言·qt
凤枭香8 分钟前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
ULTRA??12 分钟前
C加加中的结构化绑定(解包,折叠展开)
开发语言·c++
远望清一色29 分钟前
基于MATLAB的实现垃圾分类Matlab源码
开发语言·matlab
confiself38 分钟前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
小码的头发丝、1 小时前
Django中ListView 和 DetailView类的区别
数据库·python·django
XiaoLeisj1 小时前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee
杜杜的man1 小时前
【go从零单排】go中的结构体struct和method
开发语言·后端·golang