KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jgroups.tests;
4
5
6 import java.net.DatagramPacket JavaDoc;
7 import java.net.InetAddress JavaDoc;
8 import java.net.MulticastSocket JavaDoc;
9
10
11
12
13 /**
14  Tests IP multicast. Start one or more instances of McastReceiverTest 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  @see McastSenderTest
20  @author Bela Ban
21  @version $Revision: 1.4 $
22  */

23 public class McastReceiverTest {
24
25     public static void main(String JavaDoc args[]) {
26         MulticastSocket JavaDoc sock;
27         InetAddress JavaDoc mcast_addr=null, bind_addr=null;
28         DatagramPacket JavaDoc packet;
29         byte buf[]=null;
30         byte[] recv_buf;
31         String JavaDoc tmp;
32         int port=5555;
33
34         try {
35             for(int i=0; i < args.length; i++) {
36                 tmp=args[i];
37                 if("-help".equals(tmp)) {
38                     help();
39                     return;
40                 }
41                 if("-bind_addr".equals(tmp)) {
42                     bind_addr=InetAddress.getByName(args[++i]);
43                     continue;
44                 }
45                 if("-mcast_addr".equals(tmp)) {
46                     mcast_addr=InetAddress.getByName(args[++i]);
47                     continue;
48                 }
49                 if("-port".equals(tmp)) {
50                     port=Integer.parseInt(args[++i]);
51                     continue;
52                 }
53                 help();
54                 return;
55             }
56             if(mcast_addr == null)
57                 mcast_addr=InetAddress.getByName("224.0.0.150");
58         }
59         catch(Exception JavaDoc ex) {
60             System.err.println(ex);
61             return;
62         }
63
64
65         try {
66             sock=new MulticastSocket JavaDoc(port);
67             if(bind_addr != null)
68                 sock.setInterface(bind_addr);
69             sock.joinGroup(mcast_addr);
70             System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort() + ", bind interface=" +
71                                sock.getInterface());
72
73             while(true) {
74                 buf=new byte[256];
75                 packet=new DatagramPacket JavaDoc(buf, buf.length);
76                 sock.receive(packet);
77                 recv_buf=packet.getData();
78                 System.out.println(new String JavaDoc(recv_buf) + " [sender=" + packet.getAddress().getHostAddress() +
79                                    ':' + packet.getPort() + ']');
80                 byte[] buf2="Hello from Bela".getBytes();
81                 DatagramPacket JavaDoc rsp=new DatagramPacket JavaDoc(buf2, buf2.length, packet.getAddress(), packet.getPort());
82                 sock.send(rsp);
83             }
84
85         }
86         catch(Exception JavaDoc e) {
87             System.err.println(e);
88         }
89
90     }
91
92
93     static void help() {
94         System.out.println("McastSenderTest [-bind_addr <bind address>] [-help] [-mcast_addr <multicast address>] " +
95                            "[-port <port for multicast socket>]");
96     }
97
98
99 }
100
Popular Tags