我要列出ip和mac地址局域网中的所有设备(如网络扫描器)获取局域网中的所有IP和Mac地址
我想使用java编程语言。
如果我使用我的IP地址,结果是true,但是如果我在lan中使用另一个ip地址,网络变量为空。
(例如,我的IP地址:192.168.1.7,另一个IP地址:192.168.1.8)
这里我的代码;
public static void checkHosts(String subnet) throws UnknownHostException, IOException{
int timeout=3000;
for (int i=1;i<255;i++){
String host=subnet + "." + i;
if (InetAddress.getByName(host).isReachable(timeout)){
System.out.println(host + " is reachable" + InetAddress.getByName(host));
NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getByName(host));
if(network!=null){
System.out.println(network.isUp());
byte[] mac = network.getHardwareAddress();
System.out.println(network.getDisplayName());
System.out.println(network.getName());
System.out.println(InetAddress.getByName(host).getHostName());
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int j = 0; j < mac.length; j++) {
sb.append(String.format("%02X%s", mac[j], (j < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
}
}
}
}
的可能的复制[如何获得IP的连接在同一网络(子网)列表,使用Java(http://stackoverflow.com/questions/3345857/how-to-get-a -list-的-IP连接的功能于同一网络的子网使用-java的) – jww