Search IP address in DHCP local network
-
Hello everybody
I need to connect via QTcpSocket to a server in my local network. Such server has a dynamic IP assigned by the DHCP. Therefore, I don't know the IP address, but I know its hostname.
However, if i use the example code in the QHostInfo Class documentation as follows
QHostInfo::lookupHost("myServerHostName", this, SLOT(lookedUp(QHostInfo)));
void MainWindow::lookedUp(const QHostInfo &host) { if (host.error() != QHostInfo::NoError) { qDebug() << "Lookup failed:" << host.errorString(); return; } qDebug() << "Found HOST: " << host.hostName(); const auto addresses = host.addresses(); for (const QHostAddress &address : addresses) qDebug() << "Found address:" << address.toString(); }
it will return Lookup failed: "Host not found"
If instead I useQHostInfo::lookupHost("192.168.etc.", this, SLOT(lookedUp(QHostInfo)));
It will successfully find the server I need to connect to, but this can't be my solution since in the final release of my application I won't be able to know such IP address a priori.
One possible solution would be to use a brute force approach for every possible IP address, but it would be way too slow.
Another solution could be to try to get all IP addresses on the LAN (maybe by interrogating the DHCP server). Then I would brute force on a much smaller number of addresses and it would be fast enough, but I don't know hot to do it and HERE they say it is not a reliable way and do not give a solution to the question.
I could try to ping a broadcast and try to use QHostInfo::lookupHost on everyone that answers to my ping, but I do not know how to do it either.Can anyone help me to solve this problem?
ps: I needs to work regardless the operative system (windows/linux/macos).
-
@Davide87 said in Search IP address in DHCP local network:
Such server has a dynamic IP assigned by the DHCP. Therefore, I don't know the IP address, but I know its hostname.
It looks like your problem is about networking, not Qt-related.
You need to ensure your network provides a good way for name resolution (i.e. DNS service). If your server is assigned a dynamic IP via DHCP that a DNS entry for such server name is keep up to date. This post could be a potential guide just in case.