QT通过Tcp发送接收自定义结构体

随便写的写构体,对齐问题没考虑

#ifndef  _DATA_COMM_H_
#define  _DATA_COMM_H_
#include <stdio.h>

typedef 	int  INT32;
typedef	short SHORT;
typedef 	long  LONG;
typedef     float  FLOAT;
typedef     double   DOUBLE;
typedef	char  CHAR;
typedef	unsigned char UCHAR;
typedef 	unsigned short  UINT16;
typedef 	unsigned int UINT32;
typedef     unsigned long ULONG;
typedef	unsigned long long UINT64;

enum  Major:uint16_t
{
    english=0,
    math,
    Electronic,
    art
}; 
enum class WorkType:uint16_t
{
    teacher=0,
    police,
    WhiteCollar,
    farmers
};

typedef struct
{
    uint16_t Grade;
    Major major;
    uint16_t Room;
} Student;//
#pragma pack (1)
typedef struct
{
    uint16_t workingyears;
    WorkType workType;
    uint16_t Years;
    uint16_t Sex;
} Workers;//
#pragma pack (1)

#endif

把这些再封装到一个结构体中

#define DATA_LEN 1024
#pragma pack(1)     //设置结构体为1字节对齐
typedef struct sendData
{
    uchar header;          //帧头(2字节) uchar才能存十六进制数
    uchar CMD;
    uchar dataLength;        //数据个数(1字节),小于64
    uchar Type;              //发送类型
    char my_data[DATA_LEN];  //数据(小于64字节)     
}senddata;
#pragma pack()		//结束结构体对齐设置

发送

QByteArray byteArray;
    QByteArray sendByteArray;

    informationType = ui.lineEditSecdMessage->toPlainText().toInt();
    senddata.header = 0x5469;
    senddata.CMD = 0x0A;
    senddata.Type = informationType;
    memset(&senddata.my_data, 0, sizeof(senddata.my_data));
    switch (informationType)
    {
    case 1:
    {
        Student stu;
        stu.Grade = 9;
        stu.major = Major::Electronic;
        stu.Room = 509;
     
        byteArray.append(reinterpret_cast<char*>(&stu), sizeof(Student));
        str = byteArray.data();
        memcpy(senddata.my_data, str, byteArray.length());
        break;
    }
    case 2:
    {
        Workers wor;
        wor.Sex = 1;
        wor.workingyears = 17;
        wor.workType = WorkType::teacher;
        wor.Years = 43;
        
        byteArray.append(reinterpret_cast<char*>(&wor), sizeof(Workers));
        str = byteArray.data();
        memcpy(senddata.my_data, str, byteArray.length());
        break;
    }
    }
    senddata.dataLength = byteArray.length() + 4;
    sendByteArray.resize(senddata.dataLength);
    memcpy(sendByteArray.data(), &senddata, byteArray.length() + 4);
    tcpClient->write(sendByteArray);

接收

void serverWidget::readData(QByteArray array)
{
    ui.lineEditReceiveMessage->clear();
    ui.lineEditReceiveMessage->append(tr("接收到数据为:") + "\n");
    sendData receiveDate;
    sendData* getData;
    memset(&receiveDate, 0, sizeof(receiveDate));
    //getData = (sendData*)array.data();
    getData = (sendData*)array.data();
    QByteArray arrayTemp;
    arrayTemp = QByteArray(getData->my_data, getData->dataLength-4);
    switch (getData->Type)
    {
    case 1:
    {
        Student* student = (Student*)arrayTemp.data();
        ui.lineEditReceiveMessage->append("Grade:" + QString::number(student->Grade) + "\n");
        ui.lineEditReceiveMessage->append("Major in:" + QString::number(static_cast<int>(student->major)) + "\n");
        ui.lineEditReceiveMessage->append("Rooms:" + QString::number(student->Room) + "\n");
        break;
    }
    case 2:
    {
        Workers * worker = (Workers*)arrayTemp.data();
        ui.lineEditReceiveMessage->append("WorkingYears:" + QString::number(worker->workingyears) + "\n");
        ui.lineEditReceiveMessage->append("WorkType:" + QString::number(static_cast<int>(worker->workType)) + "\n");
        ui.lineEditReceiveMessage->append("SEX:" + QString::number(worker->Sex) + "\n");
        ui.lineEditReceiveMessage->append("Years:" + QString::number(worker->Years) + "\n");
        break;

    }
    
    }
   
}

写了一个服务端一个客户端效果如下
本文借鉴每木昔月

主要:(客户端 结构体->QByteArray->字符串->QByteArray->TCP传输->服务端解码)

要是有写的不对的地方,请指正
资源