#include <QCoreApplication>
#include <QUdpSocket>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QUdpSocket socket;
QByteArray data = "Hello, World!";
// 假设包头是数据长度,包尾是固定的结束符
quint8 header = 0xFE;
quint8 footer = 0xEF;
// 将包头、数据和包尾组合成一个字节数组
QByteArray packet;
packet.append((char*)&header, sizeof(header));
packet.append(data);
packet.append((char*)&footer, sizeof(footer));
//取包头和包尾
QString str;
str.sprintf("%x %x",packet.at(0) & 0xff ,packet.at(packet.length()-1) & 0xff);
qDebug() << str;
// 发送数据包
socket.writeDatagram(packet.data(), packet.size(), QHostAddress::Broadcast, 5678);
return a.exec();
}
06-05
2342
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-28
6388
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-12
518
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-02
4578
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)