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

View File

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