Get loopback interface more accurately

- Previous code returned wlan0 on my machine.
This commit is contained in:
Trustin Lee 2012-06-03 03:07:42 -07:00
parent ada61d4985
commit b47b54df37

View File

@ -22,6 +22,7 @@ import java.net.InetAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.net.SocketException; import java.net.SocketException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Enumeration;
public final class SocketAddresses { public final class SocketAddresses {
@ -62,9 +63,15 @@ public final class SocketAddresses {
// check if the NetworkInterface is null, this is the case on my ubuntu dev machine but not on osx and windows. // check if the NetworkInterface is null, this is the case on my ubuntu dev machine but not on osx and windows.
// if so fail back the the first interface // if so fail back the the first interface
if (loopbackIf == null) { if (loopbackIf == null) {
// use nextElement() as NetWorkInterface.getByIndex(0) returns null
try { try {
loopbackIf = NetworkInterface.getNetworkInterfaces().nextElement(); for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
e.hasMoreElements();) {
NetworkInterface nif = e.nextElement();
if (nif.isLoopback()) {
loopbackIf = nif;
break;
}
}
} catch (SocketException e) { } catch (SocketException e) {
logger.error("Failed to enumerate network interfaces", e); logger.error("Failed to enumerate network interfaces", e);
} }