Important: Please read the Qt Code of Conduct -

QUdpSocket multiple network (broadcast)

  • Hi,
    I use QUdpSocket to send some broadcast packet. My computer (Debian) has 2 different IP ( and
    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.

  • 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("").

    so i did a google search:

    @udp broadcast on only routs out 1 interface @

    and the second result looks interesting:


    the first answer by juliano :

    bq. First of all, you should consider broadcast obsolete, specially INADDR_BROADCAST ( 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 (/24) masks, the broadcast addresses are and 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:

  • Thank for quick replay...
    I try this:

    and not work for me.

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

  • 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 ?
    could there be a firewall switched-on on the interface that doesn't work ?

  • 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);


  • 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 brd + dev eth0


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

Log in to reply