001: // $Id: McastReceiverTest1_4.java,v 1.7 2005/08/08 14:58:36 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import java.net.DatagramPacket;
006: import java.net.InetAddress;
007: import java.net.MulticastSocket;
008: import java.net.NetworkInterface;
009: import java.util.Enumeration;
010:
011: /**
012: * Tests IP multicast. Start one or more instances of McastReceiverTest1_4 which listen for IP mcast packets
013: * and then start McastSenderTest, which sends IP mcast packets (all have to have the same IPMCAST address and port).
014: * A TTL of 0 for the McastSenderTest means that packets will only be sent to receivers on the same host. If TTL > 0,
015: * other hosts will receive the packets too. Since many routers are dropping IPMCAST traffic, this is a good way to
016: * test whether IPMCAST works between different subnets.
017: * This class compiles and runs only under JDK 1.4 or higher
018: *
019: * @author Bela Ban
020: * @version $Revision: 1.7 $
021: * @see McastSenderTest
022: */
023: public class McastReceiverTest1_4 {
024:
025: public static void main(String args[]) {
026: InetAddress mcast_addr = null, bind_addr = null;
027: String tmp;
028: int port = 5555;
029: boolean use_all_interfaces = false;
030:
031: try {
032: for (int i = 0; i < args.length; i++) {
033: tmp = args[i];
034: if ("-help".equals(tmp)) {
035: help();
036: return;
037: }
038: if ("-bind_addr".equals(tmp)) {
039: bind_addr = InetAddress.getByName(args[++i]);
040: continue;
041: }
042: if ("-mcast_addr".equals(tmp)) {
043: mcast_addr = InetAddress.getByName(args[++i]);
044: continue;
045: }
046: if ("-port".equals(tmp)) {
047: port = Integer.parseInt(args[++i]);
048: continue;
049: }
050: if ("-use_all_interfaces".equals(tmp)) {
051: use_all_interfaces = true;
052: continue;
053: }
054: help();
055: return;
056: }
057: if (mcast_addr == null)
058: mcast_addr = InetAddress.getByName("224.0.0.150");
059: } catch (Exception ex) {
060: System.err.println(ex);
061: return;
062: }
063:
064: try {
065: if (use_all_interfaces) {
066: for (Enumeration en = NetworkInterface
067: .getNetworkInterfaces(); en.hasMoreElements();) {
068: NetworkInterface intf = (NetworkInterface) en
069: .nextElement();
070: for (Enumeration e2 = intf.getInetAddresses(); e2
071: .hasMoreElements();) {
072: bind_addr = (InetAddress) e2.nextElement();
073: // System.out.println("Binding multicast socket to " + bind_addr);
074: Receiver r = new Receiver(mcast_addr,
075: bind_addr, port);
076: r.start();
077: }
078: }
079: } else {
080: Receiver r = new Receiver(mcast_addr, bind_addr, port);
081: r.start();
082: }
083: } catch (Exception e) {
084: System.err.println(e);
085: }
086:
087: }
088:
089: static void help() {
090: System.out
091: .println("McastSenderTest [-bind_addr <bind address>] [-help] [-mcast_addr <multicast address>] "
092: + "[-port <port for multicast socket>] [-use_all_interfaces (JDK 1.4 only)]");
093: }
094:
095: static class Receiver extends Thread {
096: MulticastSocket sock = null;
097: DatagramPacket packet;
098: byte buf[] = null;
099: byte[] recv_buf;
100:
101: Receiver(InetAddress mcast_addr, InetAddress bind_interface,
102: int port) throws Exception {
103: sock = new MulticastSocket(port);
104: if (bind_interface != null)
105: sock.setInterface(bind_interface);
106: sock.joinGroup(mcast_addr);
107: System.out.println("Socket=" + sock.getLocalAddress() + ':'
108: + sock.getLocalPort() + ", bind interface="
109: + sock.getInterface());
110: }
111:
112: public void run() {
113: while (true) {
114: try {
115: buf = new byte[256];
116: packet = new DatagramPacket(buf, buf.length);
117: sock.receive(packet);
118: recv_buf = packet.getData();
119: System.out.println(new String(recv_buf)
120: + " [sender="
121: + packet.getAddress().getHostAddress()
122: + ':' + packet.getPort() + ']');
123: } catch (Exception ex) {
124: System.err.println("Receiver terminated: " + ex);
125: break;
126: }
127: }
128: }
129: }
130:
131: }
|