QUdpSocket Rebroadcaster

  • I'm developing a simple tray application which acts as a UDP socket re-broadcaster.

    One thing I have realised is that as I'm listening for UDP broadcasts, any re-broadcast sent out from my application is automatically picked up again and then re-broadcasted again and again and again.

    The setup code I have is as follows is:

    // Create a QUdpSocket to listen for Oculus status messages
    m_listener = new QUdpSocket(this);
    connect(m_listener, SIGNAL(readyRead()), this, SLOT(readDatagrams()));
    m_listener->bind(52102, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);

    The code to read and re-broadcast is as follows:

    // Read through any available datagrams
    while (m_listener->hasPendingDatagrams())
        // Read the datagram out of the socket buffer
        QByteArray rx;
        m_listener->readDatagram(rx.data(), rx.size());
        // Validate and modify the UDP packet...
        m_listener->writeDatagram(rx, QHostAddress::Broadcast, 52102);

    One possible solution could be to disconnect the readyRead signal during the writeDatagram phase so that it is not re-received by my application but this seems a little numb. Another possible solution would be to check the "source IP" address during the readyRead slot.

    Are there any better solutions?

  • Qt Champions 2018

    instead of using m_listener->readDatagram you could use m_listener->receiveDatagram that contains the sender address. then just compare the sender address with yours and if they are the same ignore it

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.