1 3 package org.jgroups.tests; 4 5 import java.io.DataInputStream ; 6 import java.net.DatagramPacket ; 7 import java.net.DatagramSocket ; 8 import java.net.InetAddress ; 9 import java.util.StringTokenizer ; 10 11 12 13 17 public class McastTest { 18 19 public static void main(String args[]) { 20 DatagramSocket sock; 21 DatagramPacket packet; 22 byte[] buf=new byte[0]; 23 boolean receiver=true; 24 String val; 25 InetAddress 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 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 (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 (); 65 System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort()); 66 System.out.println("starting as sender"); 67 senderLoop(sock); 68 } 69 } 70 catch(Exception e) { 71 System.err.println(e); 72 } 73 74 } 75 76 77 static void receiverLoop(DatagramSocket sock) { 78 DatagramPacket packet; 79 byte[] buf; 80 81 while(true) { 82 buf=new byte[256]; 83 packet=new DatagramPacket (buf, buf.length); 84 try { 85 sock.receive(packet); 86 System.out.println("Receive packet from " + packet.getAddress().getHostAddress() + 87 ':' + packet.getPort() + ": " + new String (packet.getData())); 88 } 89 catch(Exception e) { 90 System.err.println(e); 91 break; 92 } 93 } 94 } 95 96 static void senderLoop(DatagramSocket sock) { 97 DataInputStream in; 98 String line; 99 DatagramPacket packet; 100 InetAddress dst; 101 int dst_port; 102 StringTokenizer tok; 103 StringBuffer sb; 104 String tmp, buf; 105 byte[] bbuf; 106 107 in=new DataInputStream (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 (line); 115 try { 116 dst=InetAddress.getByName(tok.nextToken()); 117 dst_port=Integer.parseInt(tok.nextToken()); 118 } 119 catch(Exception e) { 120 System.err.println(e); 121 continue; 122 } 123 sb=new StringBuffer (); 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 (bbuf, bbuf.length, dst, dst_port); 132 sock.send(packet); 133 } 134 catch(Exception 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 |