Java ping整个网络以查找设备
我正在尝试在5000端口上将本地网络从Java ping整个网络以查找设备,java,android,networking,arduino,Java,Android,Networking,Arduino,我正在尝试在5000端口上将本地网络从192.168.1.0ping到192.168.1.255,并且Arduino板也通过5000端口连接到网络。我有主板的Mac地址,正在尝试查找IP地址。这是我的密码 static void pingLocal() { for (int i = 0; i <= 255; i++) { ping("192.168.1." + i + ":5000"); } } private static void ping(Strin
192.168.1.0
ping到192.168.1.255
,并且Arduino板也通过5000端口连接到网络。我有主板的Mac地址,正在尝试查找IP地址。这是我的密码
static void pingLocal() {
for (int i = 0; i <= 255; i++) {
ping("192.168.1." + i + ":5000");
}
}
private static void ping(String url) {
try {
Process mIpAddrProcess = Runtime.getRuntime().exec("/system/bin/ping -c 1 " + url);
int mExitValue = mIpAddrProcess.waitFor();
System.out.println(" mExitValue " + mExitValue);
if (mExitValue == 0) {
Log.d("log", "true");
} else {
Log.d("log", "false");
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
static String getIPFromArpCache(String mac) {
if (mac == null)
return null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
Log.d("line", line);
String[] splitted = line.split(" +");
if (splitted.length >= 4 && mac.equals(splitted[3])) {
String ip = splitted[0];
if (ip.split(".").length == 4) {
return ip;
} else {
return null;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
assert br != null;
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
正如您所看到的,只有调制解调器在arp缓存中有Mac地址。怎么了?我怎样才能发现网络并从Mac中找到IP地址?我发现了这种ping网络的方法,它工作得非常好
if (InetAddress.getByName(host).isReachable(timeout)) {
System.out.println(host + " is reachable");
}
不清楚你在问什么。你在期待什么?我的主板Mac地址应该出现在arp cache@zedWhich IP地址中?它是否出现在此日志中?Ping使用ICMP,它没有端口。端口是某些第4层协议(TCP、UDP)的地址。ICMP的作用类似于第4层协议,但它实际上是IP的一部分,第3层协议,它对端口一无所知。继续@RonMaupin所说的,确保你的板是可ping的。
if (InetAddress.getByName(host).isReachable(timeout)) {
System.out.println(host + " is reachable");
}