I want to find all IP addresses of devices in the local network I'm currently connected to using Java code. The useful utility Advanced IP Scanner
is able to find various IP addresses in my subnet of 192.168.178/24
:
According to this answer, I built my code the following way:
import java.io.IOException;
import java.net.InetAddress;
public class IPScanner
{
public static void checkHosts(String subnet) throws IOException
{
int timeout = 100;
for (int i = 1; i < 255; i++)
{
String host = subnet + "." + i;
if (InetAddress.getByName(host).isReachable(timeout))
{
System.out.println(host + " is reachable");
}
}
}
public static void main(String[] arguments) throws IOException
{
checkHosts("192.168.178");
}
}
Unfortunately, this does not print out any results, meaning that no IP addresses are reachable. Why? There are devices in my local network like seen in the Advanced IP Scanner
scan.