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 multiple network (broadcast)
Forum Updated to NodeBB v4.3 + New Features

QUdpSocket multiple network (broadcast)

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 11.2k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    pou_cz
    wrote on 25 Jul 2013, 12:20 last edited by
    #1

    Hi,
    I use QUdpSocket to send some broadcast packet. My computer (Debian) has 2 different IP (192.168.0.1 and 192.168.50.1).
    And I need send broadcast packet from second IP. It's possible?
    -----When I send broadcast packet, i seen in wireshark that packet send firs IP.
    Thank for reply.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      clogwog
      wrote on 25 Jul 2013, 12:43 last edited by
      #2

      i am going to assume you do a writeDatagram to the QHostAddress::Broadcast which is:

      bq. QHostAddress::Broadcast 1 The IPv4 broadcast address. Equivalent to QHostAddress("255.255.255.255").

      so i did a google search:

      @udp broadcast on 255.255.255.255 only routs out 1 interface @

      and the second result looks interesting:

      bq. http://stackoverflow.com/questions/683624/udp-broadcast-on-all-interfaces

      the first answer by juliano :

      bq. First of all, you should consider broadcast obsolete, specially INADDR_BROADCAST (255.255.255.255). Your question highlights exactly one of the reasons that broadcast is unsuitable. It should die along with IPv4 (hopefully). Note that IPv6 doesn't even have a concept of broadcast (multicast is used, instead).

      bq. INADDR_BROADCAST is limited to the local link. Nowadays, it's only visible use is for DHCP auto-configuration, since at such time, the client will not know yet in what network it is connected to.
      With a single sendto(), only a single packet is generated, and the outgoing interface is determined by the operating system's routing table (ip route on linux). You can't have a single sendto() generate more than one packet, you would have to iterate over all interfaces, and either use raw sockets or bind the socket to a device using setsockopt(..., SOL_SOCKET, SO_BINDTODEVICE, "ethX") to send each packet bypassing the OS routing table (this requires root privileges). Not a good solution.

      bq. Instead, since INADDR_BROADCAST is not routed anyway, you can achieve almost the same thing by iterating over each interface, and sending the packet to its broadcast address. For example, assuming that your networks have 255.255.255.0 (/24) masks, the broadcast addresses are 192.168.1.255 and 192.168.2.255. Call sendto() once for each of these addresses and you will have accomplished your goal.

      i guess you would have to do two writeDatagram to:
      192.168.0.255
      and
      192.168.50.255

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pou_cz
        wrote on 25 Jul 2013, 13:16 last edited by
        #3

        Thank for quick replay...
        I try this:
        192.168.0.255
        and
        192.168.50.255

        and not work for me.

        Wireshark:
        source destination protocol length info
        192.168.0.175 192.168.54.255 UDP 47 Source port: 64501 Destination port: 64501

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clogwog
          wrote on 25 Jul 2013, 13:39 last edited by
          #4

          mmm , some more questions:

          so the 192.168.50. interface doesn't send the packet ?
          i must assume wireshark is recording on all interfaces ?
          is the network mask for 192.168.50 interface 255.255.255.0 ?
          could there be a firewall switched-on on the interface that doesn't work ?

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on 26 Jul 2013, 05:23 last edited by
            #5

            Probably could be helpful
            @void MainWindow::sendToAllBroadcast(QByteArray *packet)
            {
            // Get network interfaces list
            QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();

            // Interfaces iteration
            for (int i = 0; i < ifaces.size(); i++)
            {
                // Now get all IP addresses for the current interface
                QList<QNetworkAddressEntry> addrs = ifaces[i].addressEntries();
            
                // And for any IP address, if it is IPv4 and the interface is active, send the packet
                for (int j = 0; j < addrs.size(); j++)
                    if ((addrs[j].ip().protocol() == QAbstractSocket::IPv4Protocol) && (addrs[j].broadcast().toString() != ""))
                        udpManager.writeDatagram(packet->data(), packet->length(), addrs[j].broadcast(), 64501);
            }
            

            }@

            A 1 Reply Last reply 7 Dec 2018, 09:20
            1
            • P Offline
              P Offline
              pou_cz
              wrote on 9 Aug 2013, 05:43 last edited by
              #6

              Thank every one!!
              Now it's working. Problem was in setting network.
              I must add broadcast (option brd) to network setting, something like this:
              sudo ip addr add 192.168.54.1/24 brd + dev eth0

              pou

              1 Reply Last reply
              0
              • Q qxoz
                26 Jul 2013, 05:23

                Probably could be helpful
                @void MainWindow::sendToAllBroadcast(QByteArray *packet)
                {
                // Get network interfaces list
                QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();

                // Interfaces iteration
                for (int i = 0; i < ifaces.size(); i++)
                {
                    // Now get all IP addresses for the current interface
                    QList<QNetworkAddressEntry> addrs = ifaces[i].addressEntries();
                
                    // And for any IP address, if it is IPv4 and the interface is active, send the packet
                    for (int j = 0; j < addrs.size(); j++)
                        if ((addrs[j].ip().protocol() == QAbstractSocket::IPv4Protocol) && (addrs[j].broadcast().toString() != ""))
                            udpManager.writeDatagram(packet->data(), packet->length(), addrs[j].broadcast(), 64501);
                }
                

                }@

                A Offline
                A Offline
                Arnout
                wrote on 7 Dec 2018, 09:20 last edited by
                #7

                @qxoz This "works" but does not use 255.255.255.255 as broadcast address. Instead, it uses the network's broadcast address (e.g. 192.168.0.255)

                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