#include<QMetaType>
用到的结构体数组
typedef struct _POSITION_CMD
{
unsigned short nComm;
unsigned char Type;
unsigned char Cmd;
unsigned char Select;
int EL;
int Az;
}POSITION_CMD;
Q_DECLARE_METATYPE(POSITION_CMD);
//1.导入QUdpSocket头文件
#include<QUdpSocket>
2.创建 QUdpSocket 属性
QUdpSocket *m_udpsocket;
//构建槽函数,用来接收UDP发送的数据
public slots:
void receiveData();
3. new QUdpSocket ()分配内存
m_udpsocket=new QUdpSocket();
//绑定目标主机IP地址、目标主机端口号
m_udpsocket->bind(m_objHost,m_objPort);
//建立连接
connect(m_udpsocket,SIGNAL(readyRead()),this,SLOT(receiveData()));
//4.UDP接收字节数据转换为结构体数据position
void Udp::receiveData()
{
int iDataLen; // 接收数据
POSITION_CMD position;
memset(&position,0,sizeof (position));//清空数据信息
QByteArray baRect;
QHostAddress senderIp;
quint16 senderPort;
while (m_udpsocket->hasPendingDatagrams()) {
iDataLen=int(m_udpsocket->pendingDatagramSize