There is no reliable way to determine all hosts in a LAN. There are many means of guessing your neighbors, each with its own advantages and drawbacks. But you will never be sure you get all hosts. e.g you can try to ping a broadcast, but someone could not reply to the ping. So there is no way in Qt or others to do it. There are some ways which rely on commands in a terminal. You can try using nmap. Although it needs to install nmap:
nmap -sP 192.168.1.*
This does a simple ping scan in the entire subnet to see which all host's are online.
Or you can also try the following steps (Does not require installing nmap):
- Type “ipconfig” at command prompt. This will give you ip address of your own machine. For example, your machine ip address is 192.168.1.6 So your broadcast IP address is 192.168.1.255.
- Ping your broadcast IP address “ping 192.168.1.255” (may require -b on linux)
- Now type “arp –a” You will get the list of all IP addresses on your segment.
In Qt you can use QProcess to accomplish them like:
QProcess myProcess;
QString program = "nmap";
QStringList arguments;
arguments << "-sP" << "192.168.1.*";
myProcess.start(program, arguments);
myProcess.waitForFinished();
QByteArray result = myProcess.readAllStandardOutput ();
const QString all(result);