From b47b54df37fcc73218639c66992376f49134eff1 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sun, 3 Jun 2012 03:07:42 -0700 Subject: [PATCH] Get loopback interface more accurately - Previous code returned wlan0 on my machine. --- .../src/main/java/io/netty/util/SocketAddresses.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/io/netty/util/SocketAddresses.java b/common/src/main/java/io/netty/util/SocketAddresses.java index 07a7ccf97d..123ac790d3 100644 --- a/common/src/main/java/io/netty/util/SocketAddresses.java +++ b/common/src/main/java/io/netty/util/SocketAddresses.java @@ -22,6 +22,7 @@ import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; +import java.util.Enumeration; 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. // if so fail back the the first interface if (loopbackIf == null) { - // use nextElement() as NetWorkInterface.getByIndex(0) returns null try { - loopbackIf = NetworkInterface.getNetworkInterfaces().nextElement(); + for (Enumeration e = NetworkInterface.getNetworkInterfaces(); + e.hasMoreElements();) { + NetworkInterface nif = e.nextElement(); + if (nif.isLoopback()) { + loopbackIf = nif; + break; + } + } } catch (SocketException e) { logger.error("Failed to enumerate network interfaces", e); }