提交 3ff03ce6 编写于 作者: C chegar

6933618: java/net/MulticastSocket/NoLoopbackPackets.java fails when rerun

Reviewed-by: alanb
上级 ff314cac
...@@ -29,9 +29,7 @@ ...@@ -29,9 +29,7 @@
import java.util.*; import java.util.*;
import java.net.*; import java.net.*;
public class NoLoopbackPackets { public class NoLoopbackPackets {
private static int PORT = 9001;
private static String osname; private static String osname;
static boolean isWindows() { static boolean isWindows() {
...@@ -68,40 +66,47 @@ public class NoLoopbackPackets { ...@@ -68,40 +66,47 @@ public class NoLoopbackPackets {
return; return;
} }
// we will send packets to three multicast groups :- MulticastSocket msock = null;
// 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:1
//
List<SocketAddress> groups = new ArrayList<SocketAddress>();
groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), PORT));
groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), PORT));
groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), PORT));
Thread sender = new Thread(new Sender(groups));
sender.setDaemon(true); // we want sender to stop when main thread exits
sender.start();
// Now try to receive multicast packets. we should not see any of them
// since we disable loopback mode.
//
MulticastSocket msock = new MulticastSocket(PORT);
msock.setSoTimeout(5000); // 5 seconds
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
List<SocketAddress> failedGroups = new ArrayList<SocketAddress>(); List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
for (SocketAddress group : groups) { try {
msock.joinGroup(group, null); msock = new MulticastSocket();
int port = msock.getLocalPort();
try {
msock.receive(packet); // we will send packets to three multicast groups :-
// 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:1
//
List<SocketAddress> groups = new ArrayList<SocketAddress>();
groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));
groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));
groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));
Thread sender = new Thread(new Sender(groups));
sender.setDaemon(true); // we want sender to stop when main thread exits
sender.start();
// Now try to receive multicast packets. we should not see any of them
// since we disable loopback mode.
//
msock.setSoTimeout(5000); // 5 seconds
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
for (SocketAddress group : groups) {
msock.joinGroup(group, null);
try {
msock.receive(packet);
// it is an error if we receive something
failedGroups.add(group);
} catch (SocketTimeoutException e) {
// we expect this
}
// it is an error if we receive something msock.leaveGroup(group, null);
failedGroups.add(group);
} catch (SocketTimeoutException e) {
// we expect this
} }
} finally {
msock.leaveGroup(group, null); if (msock != null) try { msock.close(); } catch (Exception e) {}
} }
if (failedGroups.size() > 0) { if (failedGroups.size() > 0) {
...@@ -111,36 +116,36 @@ public class NoLoopbackPackets { ...@@ -111,36 +116,36 @@ public class NoLoopbackPackets {
throw new RuntimeException("test failed."); throw new RuntimeException("test failed.");
} }
} }
}
class Sender implements Runnable {
private List<SocketAddress> sendToGroups;
public Sender(List<SocketAddress> groups) { static class Sender implements Runnable {
sendToGroups = groups; private List<SocketAddress> sendToGroups;
}
public void run() { public Sender(List<SocketAddress> groups) {
byte[] buf = "hello world".getBytes(); sendToGroups = groups;
List<DatagramPacket> packets = new ArrayList<DatagramPacket>(); }
try { public void run() {
for (SocketAddress group : sendToGroups) { byte[] buf = "hello world".getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, group); List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
packets.add(packet);
}
MulticastSocket msock = new MulticastSocket(); try {
msock.setLoopbackMode(true); // disable loopback mode for (SocketAddress group : sendToGroups) {
for (;;) { DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
for (DatagramPacket packet : packets) { packets.add(packet);
msock.send(packet);
} }
Thread.currentThread().sleep(1000); // 1 second MulticastSocket msock = new MulticastSocket();
msock.setLoopbackMode(true); // disable loopback mode
for (;;) {
for (DatagramPacket packet : packets) {
msock.send(packet);
}
Thread.sleep(1000); // 1 second
}
} catch (Exception e) {
throw new RuntimeException(e);
} }
} catch (Exception e) {
throw new RuntimeException(e);
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册