Make sure multicast test use correct interface. This also fix #373
This commit is contained in:
parent
fc2049b5c7
commit
4e8c3638e1
@ -17,9 +17,11 @@ package org.jboss.netty.channel.socket;
|
|||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
|
import java.util.Enumeration;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
@ -70,14 +72,25 @@ public abstract class AbstractDatagramMulticastTest {
|
|||||||
|
|
||||||
int port = TestUtil.getFreePort();
|
int port = TestUtil.getFreePort();
|
||||||
|
|
||||||
NetworkInterface iface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
|
NetworkInterface iface = null;
|
||||||
|
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||||
|
while (interfaces.hasMoreElements()) {
|
||||||
|
NetworkInterface inf = interfaces.nextElement();
|
||||||
|
|
||||||
// check if the NetworkInterface is null, this is the case on my ubuntu dev machine but not on osx and windows.
|
// Only try to use interface if its not a loopback interface
|
||||||
// if so fail back the the first interface
|
if (!inf.isLoopback()) {
|
||||||
if (iface == null) {
|
|
||||||
// use nextElement() as NetWorkInterface.getByIndex(0) returns null
|
Enumeration<InetAddress> addresses = inf.getInetAddresses();
|
||||||
iface = NetworkInterface.getNetworkInterfaces().nextElement();
|
while (addresses.hasMoreElements()) {
|
||||||
|
InetAddress addr = addresses.nextElement();
|
||||||
|
if (addr instanceof Inet4Address) {
|
||||||
|
iface = inf;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.setOption("networkInterface", iface);
|
sb.setOption("networkInterface", iface);
|
||||||
sb.setOption("reuseAddress", true);
|
sb.setOption("reuseAddress", true);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user