【发布时间】:2015-12-06 15:22:16
【问题描述】:
我想使用 Java 代码查找当前连接到的本地网络中设备的所有 IP 地址。有用的实用程序Advanced IP Scanner 能够在我的subnet 或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 地址。为什么?我的本地网络中有一些设备,就像在Advanced IP Scanner 扫描中看到的那样。
【问题讨论】:
-
我检查了我网络上的代码,使用广播,只是我的 IP 或尝试“www.google.com”。总是说“无法到达”。检查这个答案:stackoverflow.com/questions/9922543/…
标签: java networking network-scan