mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-13 10:29:35 +01:00
InetAddress.isReachable is reported reliable on mac and recent java versions
Refactored NetUtils.isReachable to two functions: isReachableByEcho and isPortOpen If the first one will fail, the second one will be used
This commit is contained in:
parent
3fec636b29
commit
0990f98b14
@ -75,10 +75,7 @@ public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino.
|
||||
ports.add(0, 22);
|
||||
}
|
||||
|
||||
boolean reachable = false;
|
||||
for (Integer port : ports) {
|
||||
reachable = reachable || NetUtils.isReachable(inetAddress, port);
|
||||
}
|
||||
boolean reachable = NetUtils.isReachable(inetAddress, ports);
|
||||
if (!reachable) {
|
||||
boardPortIterator.remove();
|
||||
}
|
||||
|
@ -4,10 +4,37 @@ import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class NetUtils {
|
||||
|
||||
private static boolean isReachableByEcho(InetAddress address) {
|
||||
try {
|
||||
return address.isReachable(100);
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isReachable(InetAddress address, int port) {
|
||||
return isReachable(address, Arrays.asList(port));
|
||||
}
|
||||
|
||||
public static boolean isReachable(InetAddress address, List<Integer> ports) {
|
||||
if (isReachableByEcho(address)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean reachable = false;
|
||||
for (Integer port : ports) {
|
||||
reachable = reachable || isPortOpen(address, port);
|
||||
}
|
||||
|
||||
return reachable;
|
||||
}
|
||||
|
||||
private static boolean isPortOpen(InetAddress address, int port) {
|
||||
Socket socket = null;
|
||||
try {
|
||||
socket = new Socket();
|
||||
|
Loading…
x
Reference in New Issue
Block a user