You can't step in the same Enumeration twice.

This commit is contained in:
akwizgran
2011-10-30 22:35:46 +00:00
parent 7065f54a57
commit 1b0bd6961a
2 changed files with 16 additions and 17 deletions

View File

@@ -12,6 +12,7 @@ import java.net.SocketTimeoutException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.logging.Level; import java.util.logging.Level;
@@ -136,28 +137,28 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
private InetAddress chooseMulticastInterface() throws IOException { private InetAddress chooseMulticastInterface() throws IOException {
// Try to find a LAN interface that supports multicast // Try to find a LAN interface that supports multicast
Enumeration<NetworkInterface> ifaces = List<NetworkInterface> ifaces =
NetworkInterface.getNetworkInterfaces(); Collections.list(NetworkInterface.getNetworkInterfaces());
for(NetworkInterface iface : Collections.list(ifaces)) { for(NetworkInterface iface : ifaces) {
if(iface.supportsMulticast()) { if(iface.supportsMulticast()) {
Enumeration<InetAddress> addrs = iface.getInetAddresses(); Enumeration<InetAddress> addrs = iface.getInetAddresses();
for(InetAddress addr : Collections.list(addrs)) { for(InetAddress addr : Collections.list(addrs)) {
if(addr.isLinkLocalAddress() || addr.isSiteLocalAddress()) { if(addr.isLinkLocalAddress() || addr.isSiteLocalAddress()) {
if(LOG.isLoggable(Level.INFO)) if(LOG.isLoggable(Level.INFO))
LOG.info("Binding to " + addr.getHostAddress()); LOG.info("Preferring " + addr.getHostAddress());
return addr; return addr;
} }
} }
} }
} }
// Settle for a WAN interface that supports multicast // Settle for a WAN interface that supports multicast
for(NetworkInterface iface : Collections.list(ifaces)) { for(NetworkInterface iface : ifaces) {
if(iface.supportsMulticast()) { if(iface.supportsMulticast()) {
Enumeration<InetAddress> addrs = iface.getInetAddresses(); Enumeration<InetAddress> addrs = iface.getInetAddresses();
for(InetAddress addr : Collections.list(addrs)) { for(InetAddress addr : Collections.list(addrs)) {
if(!addr.isLoopbackAddress()) { if(!addr.isLoopbackAddress()) {
if(LOG.isLoggable(Level.INFO)) if(LOG.isLoggable(Level.INFO))
LOG.info("Binding to " + addr.getHostAddress()); LOG.info("Accepting " + addr.getHostAddress());
return addr; return addr;
} }
} }

View File

@@ -8,7 +8,7 @@ import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -74,30 +74,28 @@ class SimpleSocketPlugin extends SocketPlugin {
} }
protected InetAddress chooseTcpInterface(boolean lan) throws IOException { protected InetAddress chooseTcpInterface(boolean lan) throws IOException {
Enumeration<NetworkInterface> ifaces = List<NetworkInterface> ifaces =
NetworkInterface.getNetworkInterfaces(); Collections.list(NetworkInterface.getNetworkInterfaces());
// Try to find an interface of the preferred type (LAN or WAN) // Try to find an interface of the preferred type (LAN or WAN)
for(NetworkInterface iface : Collections.list(ifaces)) { for(NetworkInterface iface : ifaces) {
Enumeration<InetAddress> addrs = iface.getInetAddresses(); for(InetAddress addr : Collections.list(iface.getInetAddresses())) {
for(InetAddress addr : Collections.list(addrs)) {
if(!addr.isLoopbackAddress()) { if(!addr.isLoopbackAddress()) {
boolean link = addr.isLinkLocalAddress(); boolean link = addr.isLinkLocalAddress();
boolean site = addr.isSiteLocalAddress(); boolean site = addr.isSiteLocalAddress();
if(lan == (link || site)) { if(lan == (link || site)) {
if(LOG.isLoggable(Level.INFO)) if(LOG.isLoggable(Level.INFO))
LOG.info("Binding to " + addr.getHostAddress()); LOG.info("Preferring " + addr.getHostAddress());
return addr; return addr;
} }
} }
} }
} }
// Settle for an interface that's not of the preferred type // Settle for an interface that's not of the preferred type
for(NetworkInterface iface : Collections.list(ifaces)) { for(NetworkInterface iface : ifaces) {
Enumeration<InetAddress> addrs = iface.getInetAddresses(); for(InetAddress addr : Collections.list(iface.getInetAddresses())) {
for(InetAddress addr : Collections.list(addrs)) {
if(!addr.isLoopbackAddress()) { if(!addr.isLoopbackAddress()) {
if(LOG.isLoggable(Level.INFO)) if(LOG.isLoggable(Level.INFO))
LOG.info("Binding to " + addr.getHostAddress()); LOG.info("Accepting " + addr.getHostAddress());
return addr; return addr;
} }
} }