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. QUdpSocket Rebroadcaster
Qt 6.11 is out! See what's new in the release blog

QUdpSocket Rebroadcaster

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 694 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.
  • webzoidW Offline
    webzoidW Offline
    webzoid
    wrote on last edited by
    #1

    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;
    
        rx.resize(m_listener->pendingDatagramSize());
        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?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3

      • Login

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