[Interest] Simple device discovery using UDP
Jason H
jhihn at gmx.com
Tue Oct 2 15:43:39 CEST 2018
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?
More information about the Interest
mailing list