如何使用 Java 获取在同一网络(子网)中连接的 IP 列表 [英] How to get a list of IP connected in same network (subnet) using Java

查看:22
本文介绍了如何使用 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%29http://blog.taragana.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/

希望有帮助

这篇关于如何使用 Java 获取在同一网络(子网)中连接的 IP 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆