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