Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Send AND receive a broadcast message
QtWS25 Last Chance

Send AND receive a broadcast message

Scheduled Pinned Locked Moved General and Desktop
broadcastnetworkudp
1 Posts 1 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sk2212
    wrote on last edited by sk2212
    #1

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved