【发布时间】:2017-09-26 12:45:03
【问题描述】:
我正在使用 Qt 5.9,并且正在使用双向(发送和接收)QUdpSocket。 如何避免收到刚刚在同一个套接字上发送的相同消息?
这里是sn-p的代码
// 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());
}
}
【问题讨论】: