Send AND receive a broadcast message
-
Hello,
following code does not work:
udpSocketSend = new QUdpSocket(this); udpSocketSend->connectToHost(QHostAddress("192.168.3.255"), 8090); QHostAddress *host = new QHostAddress("192.168.3.2"); udpSocketGet = new QUdpSocket(this); udpSocketGet->bind(*host, udpSocketSend->localPort()); connect(udpSocketGet, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); connect(startButton, SIGNAL(clicked()), this, SLOT(startBroadcasting())); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram())); void Sender::readPendingDatagrams() { while (udpSocketGet->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSocketGet->pendingDatagramSize()); udpSocketGet->readDatagram(datagram.data(), datagram.size()); statusLabel->setText(tr("Received datagram: \"%1\"") .arg(datagram.data())); } } void Sender::broadcastDatagram() { statusLabel->setText(tr("Now broadcasting datagram %1").arg(messageNo)); QByteArray datagram = "<deviceInformationRequest/>"; udpSocketSend->write(datagram.data(), datagram.size()); ++messageNo; }
In Wireshark log I can see that the write with datagram '<deviceInformationRequest/>' is performed successfully. Furthermore the devices in the network which process the datagram were responding correctly with a special response message.
The problem is now that it seems that 'readPendingDatagrams()' is never fired. Therefore I am not able to parse request message.
What is the problem? This is an extended example from given QT "Sender" Broadcast example http://doc.qt.io/qt-5/qtnetwork-broadcastsender-example.html