如何使用Java获取在同一网络(子网)中连接的IP列表

问题描述:

如何使用Java获取连接到同一子网的设备的IP地址列表?

How do I get list of IP addresses for devices connected to my same subnet using Java?

这应该适用于网络上的主机对ICMP包(ping)(> JDK 5)作出反应:

this should work when the hosts on your network react to ICMP packages (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");
       }
   }
}

调用子网的方法(192.168.0.1-254)像这样:

invoke the method for a subnet (192.168.0.1-254) like this:

checkHosts("192.168.0");

没有测试它但应该有点像这样工作。显然这只检查ip地址的最后一个字节中的254个主机...

didnt test it but should work kinda like this. Obviously this only checks the 254 hosts in the last byte of the ip address...

检查:

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-ping-in-java-jdk-15-and-above /

希望有所帮助