我的代码每次到达创建字节数组时都会挂起,我似乎无法弄清楚原因。请原谅我一直在尝试的一切评论。我得到一个空指针异常。到目前为止,这是我的方法。
@SuppressWarnings("static-access")
public static void NetworkScan() throws IOException{
InetAddress ip;
ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());
String ipstring =ip.getHostAddress();
ipstring = ipstring.substring(0, 10);
//StringBuilder ipstring2 = new StringBuilder(ipstring.substring(0, 12));
//System.out.println(ipstring);
for(int i=100; i<=200; i++){// ip range for routers is 100-200
String ipstring2 = ipstring + i;
//System.out.println(ipstring2);
ip = ip.getByName(ipstring2);
//System.out.println(ip.getHostAddress());
if(ip.isReachable(5000)){
try {
//ip = InetAddress.getLocalHost();
//System.out.println("Current IP address : " + ip.getHostAddress());
//NetworkInterface network = NetworkInterface.getByInetAddress(ip);
NetworkInterface network = NetworkInterface.getByName(ip.getHostAddress());
byte[] mac = network.getHardwareAddress();
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());
writeFile(sb);
}catch (SocketException e){
e.printStackTrace();
}//end try catch
}else{//end if
System.out.println(ip.getHostAddress() + " IP is not reachable");
}
}
}//end NetworkScan
我已经查看了与我类似的其他代码,他们似乎没有这个问题,但这可能是我一直在寻找的东西。