Important: Please read the Qt Code of Conduct -

Send AND receive a broadcast message

  • Hello,

    following code does not work:

    udpSocketSend = new QUdpSocket(this);
    udpSocketSend->connectToHost(QHostAddress(""), 8090);
    QHostAddress *host = new QHostAddress("");
    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;
            udpSocketGet->readDatagram(, datagram.size());
            statusLabel->setText(tr("Received datagram: \"%1\"")
    void Sender::broadcastDatagram()
        statusLabel->setText(tr("Now broadcasting datagram %1").arg(messageNo));
        QByteArray datagram = "<deviceInformationRequest/>";
        udpSocketSend->write(, datagram.size());

    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

Log in to reply