QUdpSocket multiple network (broadcast)
-
wrote on 25 Jul 2013, 12:20 last edited by
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. -
wrote on 25 Jul 2013, 12:43 last edited by
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 -
wrote on 25 Jul 2013, 13:16 last edited by
Thank for quick replay...
I try this:
192.168.0.255
and
192.168.50.255and 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 -
wrote on 25 Jul 2013, 13:39 last edited by
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 ? -
wrote on 26 Jul 2013, 05:23 last edited by
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); }
}@
-
wrote on 9 Aug 2013, 05:43 last edited by
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 eth0pou
-
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); }
}@