我使用以下代码
InetAddress localhost = InetAddress.getLocalHost();
// this code assumes IPv4 is used
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++)
{
ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(1000))
{
System.out.println(address + " machine is turned on and can be pinged");
}
else if (!address.getHostAddress().equals(address.getHostName()))
{
System.out.println(address + " machine is known in a DNS lookup");
}
else
{
System.out.println(address + " the host address and host name " +
"are equal, meaning the host name could not be resolved");
}
}
尝试查找我的wifi网络上的所有在线设备。 但是,出现了两个问题。
解决这个难题的任何帮助?
答案 0 :(得分:0)
我用oracle jdk 1.8.0_51尝试了类似的代码并且工作正常;但是当我在oracle jre 1.8.0_91上测试它时,所有网段的ip都会恢复正常。
对于当前的jre版本,可能是InetAddress.isReachable(1000)的Bug,在openjdk上报告了类似的错误https://bugs.openjdk.java.net/browse/JDK-8145011