1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

NetworkDiscovery: InetAddress.isReachable() is unreliable, let's open a socket

This commit is contained in:
Federico Fissore 2013-07-19 15:17:27 +02:00
parent 8f59bc13c5
commit def612e7b7
2 changed files with 30 additions and 5 deletions

View File

@ -4,13 +4,13 @@ import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Discovery;
import cc.arduino.packages.discoverers.network.NetworkChecker;
import processing.app.Base;
import processing.app.helpers.NetUtils;
import processing.app.helpers.PreferencesMap;
import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter;
import javax.jmdns.*;
import javax.jmdns.impl.DNSTaskStarter;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
@ -33,14 +33,11 @@ public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino.
Iterator<BoardPort> iterator = ports.iterator();
while (iterator.hasNext()) {
try {
InetAddress address = Inet4Address.getByName(iterator.next().getAddress());
if (!address.isReachable(100)) {
if (!NetUtils.isReachable(InetAddress.getByName(iterator.next().getAddress()))) {
iterator.remove();
}
} catch (UnknownHostException e) {
iterator.remove();
} catch (IOException e) {
iterator.remove();
}
}
return ports;

View File

@ -0,0 +1,28 @@
package processing.app.helpers;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
public abstract class NetUtils {
public static boolean isReachable(InetAddress address) {
Socket socket = null;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(address, 80), 100);
return true;
} catch (IOException e) {
return false;
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// noop
}
}
}
}
}