Discussion:
[Interest] Simple device discovery using UDP
Jason H
2018-10-02 13:43:39 UTC
Permalink
I have an app (Desktop) that I want it to know about other running instances on the local network. I figured UDP broadcast was a natural choice. I tried it, but I never saw anything but my own (local) packets. There's quite a few stackoverflow questions, but none used Qt. I'm wondering if it's a wifi/router thing?

The hosts are on the same WiFi SSID and address space:
10.31.4.26 host1
10.31.4.202 host2

Both are on my desk.
HostInfoService::HostInfoService(...) {
...
udpSocket4.bind(QHostAddress::Any, 13999, QUdpSocket::ShareAddress);
connect(&udpSocket4, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
...
}

void HostInfoService::readPendingDatagrams()
{
for (auto udpSocket: {&udpSocket4 } ) { //, &udpSocket6}) {
while (udpSocket->hasPendingDatagrams()) {
QNetworkDatagram datagram = udpSocket->receiveDatagram();
QString host = QString(datagram.data().constData());
messages[host] = datagram.senderAddress();
qDebug() << Q_FUNC_INFO << host << datagram.senderAddress().toString();
}
}
}

I also tried on an IPv6 socket, no joy.

Has anyone else done something like this?
Jason H
2018-10-02 13:44:48 UTC
Permalink
Forgot to include my sending code, incase that's the problem:

void HostInfoService::update() {
QNetworkDatagram datagram(QHostInfo::localHostName().toLocal8Bit(), QHostAddress::Broadcast, 13999);
udpSocket4.writeDatagram(datagram);
//udpSocket6.writeDatagram(datagram);
qDebug() << Q_FUNC_INFO << QHostInfo::localHostName().toLocal8Bit();
}
Sent: Tuesday, October 02, 2018 at 9:43 AM
Subject: [Interest] Simple device discovery using UDP
I have an app (Desktop) that I want it to know about other running instances on the local network. I figured UDP broadcast was a natural choice. I tried it, but I never saw anything but my own (local) packets. There's quite a few stackoverflow questions, but none used Qt. I'm wondering if it's a wifi/router thing?
10.31.4.26 host1
10.31.4.202 host2
Both are on my desk.
HostInfoService::HostInfoService(...) {
...
udpSocket4.bind(QHostAddress::Any, 13999, QUdpSocket::ShareAddress);
connect(&udpSocket4, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
...
}
void HostInfoService::readPendingDatagrams()
{
for (auto udpSocket: {&udpSocket4 } ) { //, &udpSocket6}) {
while (udpSocket->hasPendingDatagrams()) {
QNetworkDatagram datagram = udpSocket->receiveDatagram();
QString host = QString(datagram.data().constData());
messages[host] = datagram.senderAddress();
qDebug() << Q_FUNC_INFO << host << datagram.senderAddress().toString();
}
}
}
I also tried on an IPv6 socket, no joy.
Has anyone else done something like this?
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
Murphy, Sean
2018-10-02 14:01:46 UTC
Permalink
Post by Jason H
I have an app (Desktop) that I want it to know about other running instances
on the local network. I figured UDP broadcast was a natural choice. I tried it,
but I never saw anything but my own (local) packets. There's quite a few
stackoverflow questions, but none used Qt. I'm wondering if it's a wifi/router
thing?
A couple of first questions:
- do you have any sort of firewalls running anywhere?
- have you tried the Qt Networks examples, and do those work?
http://doc.qt.io/qt-5/qtexamples.html#qt-network, maybe the Broadcast
Sender & Receiver examples

Sean


This message has been scanned for malware by Forcepoint. www.forcepoint.com
coroberti .
2018-10-02 15:14:01 UTC
Permalink
Post by Murphy, Sean
Post by Jason H
I have an app (Desktop) that I want it to know about other running instances
on the local network. I figured UDP broadcast was a natural choice. I tried it,
but I never saw anything but my own (local) packets. There's quite a few
stackoverflow questions, but none used Qt. I'm wondering if it's a wifi/router
thing?
- do you have any sort of firewalls running anywhere?
- have you tried the Qt Networks examples, and do those work?
http://doc.qt.io/qt-5/qtexamples.html#qt-network, maybe the Broadcast
Sender & Receiver examples
Sean
Hi,
1. As Sean has mentioned, firewall settings that do not allow broadcasts
could be the case.

2. Yet another case could be: QHostAddress::Broadcast.
It's so-called All-Networks-Broadcast (255.255.255.255)
and normally requires alleviated privileges to send to (UNIX root, suid)
and could be guarded.

There's another broadcast address - so-called LAN-broadcast
which is less sensitive and more appropriate for broadcasting.

For example, at network 10.31.4.0/24, it will be 10.31.4.255,
and you can find various rules to calculate it.

3. Instead of using a broadcast address, you can figure our all
addresses on this network
and send unicast requests except to the subnet broadcast address .
At most networks, it will be sending to up to 253 address, whereas
rarely it could come to higher numbers.

Take care,

Kind regards,
Robert
............................
Thiago Macieira
2018-10-02 17:28:30 UTC
Permalink
Post by coroberti .
Hi,
1. As Sean has mentioned, firewall settings that do not allow broadcasts
could be the case.
2. Yet another case could be: QHostAddress::Broadcast.
It's so-called All-Networks-Broadcast (255.255.255.255)
and normally requires alleviated privileges to send to (UNIX root, suid)
and could be guarded.
There's another broadcast address - so-called LAN-broadcast
which is less sensitive and more appropriate for broadcasting.
For example, at network 10.31.4.0/24, it will be 10.31.4.255,
and you can find various rules to calculate it.
Use QNetworkInterface dor that.

But instead of using broadcast, use multicast. Then only the devices that are
interested in being discovered will join the multicast group. That will also
work in IPv6.
Post by coroberti .
3. Instead of using a broadcast address, you can figure our all
addresses on this network
and send unicast requests except to the subnet broadcast address .
At most networks, it will be sending to up to 253 address, whereas
rarely it could come to higher numbers.
Please don't do that. That's practically "port scanning" and poking at ports
in other people's devices is impolite. Your netadmin may come knocking on your
door. Like a proper port scanning, you'd need to do proper throttling and
resending, to avoid packet loss due to collisions. Expecting that you're only
in a /24 network is also a bad design -- here in the office, the WiFi is on a
/22 so you go from 254 to 1022 IPs to scan. And of course, the solution falls
completely flat on IPv6: we're in 2018, not 2001, so designing any solution
that fails to work in IPv6 is malpractice.

If you really want to do this, install nmap.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Michael Jackson
2018-10-02 15:19:07 UTC
Permalink
What you want is this:

https://github.com/nitroshare/qmdnsengine

Don't reinvent the wheel if you don't have to. MIT licensed. No relation to the project at all.

--
Michael Jackson | Owner, President
BlueQuartz Software
[e] ***@bluequartz.net
[w] www.bluequartz.net <http://www.bluequartz.net>

On 10/2/18, 9:43 AM, "Interest on behalf of Jason H" <interest-bounces+mike.jackson=***@qt-project.org on behalf of ***@gmx.com> wrote:

I have an app (Desktop) that I want it to know about other running instances on the local network. I figured UDP broadcast was a natural choice. I tried it, but I never saw anything but my own (local) packets. There's quite a few stackoverflow questions, but none used Qt. I'm wondering if it's a wifi/router thing?

The hosts are on the same WiFi SSID and address space:
10.31.4.26 host1
10.31.4.202 host2

Both are on my desk.
HostInfoService::HostInfoService(...) {
...
udpSocket4.bind(QHostAddress::Any, 13999, QUdpSocket::ShareAddress);
connect(&udpSocket4, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
...
}

void HostInfoService::readPendingDatagrams()
{
for (auto udpSocket: {&udpSocket4 } ) { //, &udpSocket6}) {
while (udpSocket->hasPendingDatagrams()) {
QNetworkDatagram datagram = udpSocket->receiveDatagram();
QString host = QString(datagram.data().constData());
messages[host] = datagram.senderAddress();
qDebug() << Q_FUNC_INFO << host << datagram.senderAddress().toString();
}
}
}

I also tried on an IPv6 socket, no joy.

Has anyone else done something like this?

_______________________________________________
Interest mailing list
***@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest
Thiago Macieira
2018-10-02 17:21:35 UTC
Permalink
Post by Jason H
I have an app (Desktop) that I want it to know about other running instances
on the local network. I figured UDP broadcast was a natural choice. I tried
it, but I never saw anything but my own (local) packets. There's quite a
few stackoverflow questions, but none used Qt. I'm wondering if it's a
wifi/router thing?
It could be.

In a network problem, ALWAYS isolate whether the problem is the sender or the
receiver. Please run a packet capture on both the sender and the receiver
machines, letting us know whether the sender sent the packet and whether the
receiver received it.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Continue reading on narkive:
Loading...