0
我正在使用Qt 5.9,我正在使用双向(发送和接收)QUdpSocket。 我怎样才能避免接收同一个消息刚刚发送到同一个套接字?QUdpSocket环回数据报
下面的代码
// Socket init
this->UdpSocket->bind(QHostAddress::Any, ARTNET_PROTOCOL_PORT);
connect(this->UdpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()), Qt::UniqueConnection);
[...]
void ArtNetManager::readPendingDatagrams()
{
QNetworkDatagram networkDatagram;
qDebug("Udp datagram received");
while(this->UdpSocket->hasPendingDatagrams())
{
networkDatagram = this->UdpSocket->receiveDatagram();
qDebug("Received datagram from IP address: %s", networkDatagram.senderAddress().toString().toLatin1().data());
this->receiveDatagram(networkDatagram.data());
}
}
void ArtNetManager::sendDatagram()
{
QByteArray ArtNet_RawMsg;
ArtNet_RawMsg.append("Test program");
// Writes data on the UDP socket
qint64 sentBytes = this->UdpSocket->writeDatagram(ArtNet_RawMsg, QHostAddress::Broadcast, ARTNET_PROTOCOL_PORT);
if(sentBytes == -1)
{
qDebug("Cannot send data on UPD socket. Error: %d", this->UdpSocket->error());
}
else if(sentBytes != ArtNet_RawMsg.size())
{
qDebug("Wrong number of bytes sent. Bytes sent on socket: %d, tx buffer length: %d", sentBytes, ArtNet_RawMsg.size());
}
}
不,它不是这样做的原因。即使我将连接PC与ArtNet主机设备的以太网电缆断开连接,我的程序在发送它们之后仍会立即收到消息。我已经将qDebug调用放入'readPendingDatagrams'函数中,以检查消息的发送者:IP地址与我在PC上设置的IP地址相同,因此这确认双向QUdpSocket接收它发送的消息。 – Andre
Your网络设置搞砸了。这不是正常的行为。那是什么平台(非常精确):? –