KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

25 public class McastSenderTest {
26
27     public static void main(String JavaDoc args[]) {
28         MulticastSocket JavaDoc sock;
29         InetAddress JavaDoc mcast_addr=null, bind_addr=null;
30         DatagramPacket JavaDoc packet;
31         byte[] buf=new byte[0];
32         String JavaDoc tmp;
33         int ttl=32;
34         String JavaDoc line;
35         DataInputStream JavaDoc in;
36         AckReceiver ack_receiver=null;
37         int port=5555;
38
39
40         try {
41             for(int i=0; i < args.length; i++) {
42                 tmp=args[i];
43                 if("-help".equals(tmp)) {
44                     help();
45                     return;
46                 }
47                 if("-bind_addr".equals(tmp)) {
48                     bind_addr=InetAddress.getByName(args[++i]);
49                     continue;
50                 }
51                 if("-mcast_addr".equals(tmp)) {
52                     mcast_addr=InetAddress.getByName(args[++i]);
53                     continue;
54                 }
55                 if("-ttl".equals(tmp)) {
56                     ttl=Integer.parseInt(args[++i]);
57                     continue;
58                 }
59                 if("-port".equals(tmp)) {
60                     port=Integer.parseInt(args[++i]);
61                     continue;
62                 }
63                 help();
64                 return;
65             }
66             if(mcast_addr == null)
67                 mcast_addr=InetAddress.getByName("224.0.0.150");
68         }
69         catch(Exception JavaDoc ex) {
70             System.err.println(ex);
71             return;
72         }
73
74
75         try {
76             sock=new MulticastSocket JavaDoc(port);
77             sock.setTimeToLive(ttl);
78             if(bind_addr != null)
79                 sock.setInterface(bind_addr);
80
81             System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort() +
82                                ", ttl=" + sock.getTimeToLive() + ", bind interface=" + sock.getInterface());
83
84             ack_receiver=new AckReceiver(sock);
85             ack_receiver.start();
86             in=new DataInputStream JavaDoc(System.in);
87             while(true) {
88                 System.out.print("> ");
89                 line=in.readLine();
90                 if(line.startsWith("quit") || line.startsWith("exit")) {
91                     if(ack_receiver != null)
92                         ack_receiver.stop();
93                     break;
94                 }
95                 buf=line.getBytes();
96                 packet=new DatagramPacket JavaDoc(buf, buf.length, mcast_addr, port);
97                 sock.send(packet);
98             }
99         }
100         catch(Exception JavaDoc e) {
101             System.err.println(e);
102         }
103
104     }
105
106
107     static void help() {
108         System.out.println("McastSenderTest [-bind_addr <bind address>] [-help] [-mcast_addr <multicast address>] " +
109                            "[-port <multicast port that receivers are listening on>] [-ttl <time to live for mcast packets>]");
110     }
111
112
113     private static class AckReceiver implements Runnable JavaDoc {
114         DatagramSocket JavaDoc sock;
115         DatagramPacket JavaDoc packet;
116         byte[] buf;
117         Thread JavaDoc t=null;
118
119         AckReceiver(DatagramSocket JavaDoc sock) {
120             this.sock=sock;
121         }
122
123         public void run() {
124             while(t != null) {
125                 try {
126                     buf=new byte[256];
127                     packet=new DatagramPacket JavaDoc(buf, buf.length);
128                     sock.receive(packet);
129                     System.out.println("<< Received response from " +
130                                        packet.getAddress().getHostAddress() + ':' +
131                                        packet.getPort() + ": " + new String JavaDoc(packet.getData()));
132                 }
133                 catch(Exception JavaDoc e) {
134                     System.err.println(e);
135                     break;
136                 }
137             }
138             t=null;
139         }
140
141         void start() {
142             t=new Thread JavaDoc(this, "McastSenderTest.AckReceiver thread");
143             t.start();
144         }
145
146         void stop() {
147             if(t != null && t.isAlive()) {
148                 t=null;
149                 try {
150                     sock.close();
151                 }
152                 catch(Exception JavaDoc e) {
153                 }
154             }
155         }
156     }
157
158
159 }
160
Popular Tags