fas*_*seg 47
当网络上的主机对ICMP包(ping)(> JDK 5)作出反应时,这应该有效:
public void checkHosts(String subnet){
int timeout=1000;
for (int i=1;i<255;i++){
String host=subnet + "." + i;
if (InetAddress.getByName(host).isReachable(timeout)){
System.out.println(host + " is reachable");
}
}
}
Run Code Online (Sandbox Code Playgroud)
调用子网的方法(192.168.0.1-254),如下所示:
checkHosts("192.168.0");
Run Code Online (Sandbox Code Playgroud)
没有测试它,但应该像这样工作.显然这只检查ip地址的最后一个字节中的254个主机...
校验:
http://download-llnw.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable%28int%29 http://blog.taragana.com/index.php/archive/how -to-DO-ICMP平功能于Java的JDK-15及以上/
希望有所帮助
小智 5
要列出LAN中连接的主机,您需要ping子网上的所有可用IP地址.但ping消息可能会被防火墙限制,因此更安全的方式是打开LAN到IP地址范围内每个IP地址的套接字.