问题:标准范式。
方法:
初始化阶段:
m_udpSocket = new QUdpSocket(this);
m_udpSocket->open(QUdpSocket::ReadOnly);
m_udpSocket->bind(QHostAddress::Any, 12345);
connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));
接收数据阶段:
void handleReadyRead()
{
while (m_udpSocket->hasPendingDatagram();
{
QNetworkDatagram datagram = m_udpSocket->receiveDatagram();
QByteArray array = datagram.data();
if (quint8(array.at(0)) == 0xAA && quint8(array.at(1)) == 0xAA)
{
......
}
......
}
}
关闭连接阶段:
if (m_udpSocket && m_udpSocket->isOpen())
{
m_udpSocket->close();
delete m_udpSocket;
m_udpSocket = nullptr;
}