我希望使用Java代码查找当前连接到的本地网络中所有设备的IP地址。 有用的实用程序
很不幸,这并没有输出任何结果,这意味着没有可达的IP地址。为什么?我的本地网络中存在设备,就像在“高级IP扫描仪”扫描中看到的那样。
Advanced IP Scanner
能够在我的子网 192.168.178/24
中找到各种IP地址:
根据this的回答,我按照以下方式编写了我的代码: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");
}
}
很不幸,这并没有输出任何结果,这意味着没有可达的IP地址。为什么?我的本地网络中存在设备,就像在“高级IP扫描仪”扫描中看到的那样。