KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > McastReceiverTest1_4


1 // $Id: McastReceiverTest1_4.java,v 1.4 2004/07/05 14:15:11 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import java.net.DatagramPacket JavaDoc;
6 import java.net.InetAddress JavaDoc;
7 import java.net.MulticastSocket JavaDoc;
8 import java.net.NetworkInterface JavaDoc;
9 import java.util.Enumeration JavaDoc;
10
11
12
13 /**
14    Tests IP multicast. Start one or more instances of McastReceiverTest1_4 which listen for IP mcast packets
15    and then start McastSenderTest, which sends IP mcast packets (all have to have the same IPMCAST address and port).
16    A TTL of 0 for the McastSenderTest means that packets will only be sent to receivers on the same host. If TTL > 0,
17    other hosts will receive the packets too. Since many routers are dropping IPMCAST traffic, this is a good way to
18    test whether IPMCAST works between different subnets.
19    This class compiles and runs only under JDK 1.4 or higher
20    @see McastSenderTest
21    @author Bela Ban
22    @version $Revision: 1.4 $
23  */

24 public class McastReceiverTest1_4 {
25
26     public static void main(String JavaDoc args[]) {
27     InetAddress JavaDoc mcast_addr=null, bind_addr=null;
28     String JavaDoc tmp;
29     int port=5555;
30     boolean use_all_interfaces=false;
31
32     try {
33         for(int i=0; i < args.length; i++) {
34         tmp=args[i];
35         if("-help".equals(tmp)) {
36             help();
37             return;
38         }
39         if("-bind_addr".equals(tmp)) {
40             bind_addr=InetAddress.getByName(args[++i]);
41             continue;
42         }
43         if("-mcast_addr".equals(tmp)) {
44             mcast_addr=InetAddress.getByName(args[++i]);
45             continue;
46         }
47         if("-port".equals(tmp)) {
48             port=Integer.parseInt(args[++i]);
49             continue;
50         }
51         if("-use_all_interfaces".equals(tmp)) {
52             use_all_interfaces=true;
53             continue;
54         }
55         help();
56         return;
57         }
58         if(mcast_addr == null)
59         mcast_addr=InetAddress.getByName("224.0.0.150");
60     }
61     catch(Exception JavaDoc ex) {
62         System.err.println(ex);
63         return;
64     }
65
66     if(use_all_interfaces) {
67         if(!is1_4()) {
68         System.err.println("-use_all_interfaces flag requires JDK 1.4 or greater");
69         return;
70         }
71     }
72
73
74     try {
75         if(use_all_interfaces) {
76         for(Enumeration JavaDoc en=NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
77             NetworkInterface JavaDoc intf=(NetworkInterface JavaDoc)en.nextElement();
78             for(Enumeration JavaDoc e2=intf.getInetAddresses(); e2.hasMoreElements();) {
79             bind_addr=(InetAddress JavaDoc)e2.nextElement();
80             // System.out.println("Binding multicast socket to " + bind_addr);
81
Receiver r=new Receiver(mcast_addr, bind_addr, port);
82             r.start();
83             }
84         }
85         }
86         else {
87         Receiver r=new Receiver(mcast_addr, bind_addr, port);
88         r.start();
89         }
90     }
91     catch(Exception JavaDoc e) {
92         System.err.println(e);
93     }
94
95     }
96
97
98     static boolean is1_4() {
99     Class JavaDoc cl;
100     
101     try {
102         cl=Thread.currentThread().getContextClassLoader().loadClass("java.net.NetworkInterface");
103         return true;
104     }
105     catch(Throwable JavaDoc ex) {
106         return false;
107     }
108     }
109
110
111     static void help() {
112     System.out.println("McastSenderTest [-bind_addr <bind address>] [-help] [-mcast_addr <multicast address>] " +
113                "[-port <port for multicast socket>] [-use_all_interfaces (JDK 1.4 only)]");
114     }
115
116
117
118     static class Receiver extends Thread JavaDoc {
119     MulticastSocket JavaDoc sock=null;
120     DatagramPacket JavaDoc packet;
121     byte buf[]=null;
122     byte[] recv_buf;
123
124     Receiver(InetAddress JavaDoc mcast_addr, InetAddress JavaDoc bind_interface, int port) throws Exception JavaDoc {
125         sock=new MulticastSocket JavaDoc(port);
126         if(bind_interface != null)
127         sock.setInterface(bind_interface);
128         sock.joinGroup(mcast_addr);
129         System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort() + ", bind interface=" +
130                    sock.getInterface());
131     }
132     
133
134     public void run() {
135         while(true) {
136         try {
137             buf=new byte[256];
138             packet=new DatagramPacket JavaDoc(buf, buf.length);
139             sock.receive(packet);
140             recv_buf=packet.getData();
141             System.out.println(new String JavaDoc(recv_buf) + " [sender=" + packet.getAddress().getHostAddress() +
142                                ':' + packet.getPort() + ']');
143         }
144         catch(Exception JavaDoc ex) {
145             System.err.println("Receiver terminated: " + ex);
146             break;
147         }
148         }
149     }
150     }
151
152     
153 }
154
Popular Tags