我正在使用Qt 5.4(在Windows 10 64位上)通过udp测试P2P通信。
在Windows到Windows上,此代码可以从另一台设备获取消息(“SendFromHost”)。但是在Android到Windows上,这段代码得到了自己发送的消息(“SendFromGuest”)并完成了程序。
请告诉我如何在没有自己发送的情况下收到邮件。
void Network::start()
{
findLanSocket = new QUdpSocket(this);
connect(findLanSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this, SLOT(onUdpStateChanged(QAbstractSocket::SocketState)));
findLanSocket->bind(findPort, QUdpSocket::ShareAddress);
QByteArray datagram = "SendFromGuest";
findLanSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, findPort);
}
void Network::onUdpStateChanged(QAbstractSocket::SocketState s)
{
if (s == QAbstractSocket::BoundState) {
connect(findLanSocket, SIGNAL(readyRead()), this, SLOT(onReadyUdpRead()));
}
}
void Network::onReadyUdpRead()
{
QByteArray datagram; QHostAddress haddr;
datagram.resize(findLanSocket->pendingDatagramSize());
findLanSocket->readDatagram(datagram.data(), datagram.size(), &haddr);
QString rev = QString::fromUtf8(datagram);
if (rev == "SendFromHost"){
result = haddr.toString();
qDebug() << result;
success();
return;
}
}