我要列出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