KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jgroups.tests;
4
5 import java.io.DataInputStream JavaDoc;
6 import java.net.DatagramPacket JavaDoc;
7 import java.net.DatagramSocket JavaDoc;
8 import java.net.InetAddress JavaDoc;
9 import java.util.StringTokenizer JavaDoc;
10
11
12
13 /**
14    @author Bela Ban
15    @version $Revision: 1.3 $
16  */

17 public class McastTest {
18
19     public static void main(String JavaDoc args[]) {
20     DatagramSocket JavaDoc sock;
21     DatagramPacket JavaDoc packet;
22     byte[] buf=new byte[0];
23     boolean receiver=true;
24     String JavaDoc val;
25     InetAddress JavaDoc addr=null;
26     int port=0;
27
28     
29     for(int i=0; i < args.length; i++) {
30         val=args[i];
31         if("-help".equals(val)) {
32         help();
33         return;
34         }
35         if("-sender".equals(val)) {
36         receiver=false;
37         continue;
38         }
39         if("-receiver".equals(val)) {
40         receiver=true;
41         try {
42             addr=InetAddress.getByName(args[++i]);
43             port=Integer.parseInt(args[++i]);
44         }
45         catch(Exception JavaDoc e) {
46             System.err.println(e);
47             help();
48             return;
49         }
50         continue;
51         }
52         help();
53     }
54
55
56     try {
57         if(receiver) {
58         sock=new DatagramSocket JavaDoc(port, addr);
59         System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort());
60         System.out.println("starting as receiver");
61         receiverLoop(sock);
62         }
63         else {
64         sock=new DatagramSocket JavaDoc();
65         System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort());
66         System.out.println("starting as sender");
67         senderLoop(sock);
68         }
69     }
70     catch(Exception JavaDoc e) {
71         System.err.println(e);
72     }
73
74     }
75
76
77     static void receiverLoop(DatagramSocket JavaDoc sock) {
78     DatagramPacket JavaDoc packet;
79     byte[] buf;
80
81     while(true) {
82         buf=new byte[256];
83         packet=new DatagramPacket JavaDoc(buf, buf.length);
84         try {
85         sock.receive(packet);
86         System.out.println("Receive packet from " + packet.getAddress().getHostAddress() +
87                            ':' + packet.getPort() + ": " + new String JavaDoc(packet.getData()));
88         }
89         catch(Exception JavaDoc e) {
90         System.err.println(e);
91         break;
92         }
93     }
94     }
95     
96     static void senderLoop(DatagramSocket JavaDoc sock) {
97     DataInputStream JavaDoc in;
98     String JavaDoc line;
99     DatagramPacket JavaDoc packet;
100     InetAddress JavaDoc dst;
101     int dst_port;
102     StringTokenizer JavaDoc tok;
103     StringBuffer JavaDoc sb;
104     String JavaDoc tmp, buf;
105     byte[] bbuf;
106     
107     in=new DataInputStream JavaDoc(System.in);
108     while(true) {
109         try {
110         System.out.print("> ");
111         line=in.readLine();
112         if(line.startsWith("quit") || line.startsWith("exit"))
113             break;
114         tok=new StringTokenizer JavaDoc(line);
115         try {
116             dst=InetAddress.getByName(tok.nextToken());
117             dst_port=Integer.parseInt(tok.nextToken());
118         }
119         catch(Exception JavaDoc e) {
120             System.err.println(e);
121             continue;
122         }
123         sb=new StringBuffer JavaDoc();
124         while(tok.hasMoreTokens()) {
125             tmp=tok.nextToken();
126             sb.append(tmp + ' ');
127         }
128         buf=sb.toString();
129         System.out.println("sending " + buf);
130         bbuf=buf.getBytes();
131         packet=new DatagramPacket JavaDoc(bbuf, bbuf.length, dst, dst_port);
132         sock.send(packet);
133         }
134         catch(Exception JavaDoc e) {
135         System.err.println(e);
136         break;
137         }
138     }
139     }
140
141
142     static void help() {
143     System.out.println("McastTest [-help] [-sender | -receiver <local addr> <local port>]");
144     }
145
146     
147 }
148
Popular Tags