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 UcastTest { 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 receiver_addr=null, sender_addr=null; 26 int receiver_port=0, sender_port=0; 27 28 29 30 for(int i=0; i < args.length; i++) { 31 val=args[i]; 32 if("-help".equals(val)) { 33 help(); 34 return; 35 } 36 if("-sender".equals(val)) { 37 receiver=false; 38 continue; 39 } 40 if("-sender_addr".equals(val)) { 41 try { 42 sender_addr=InetAddress.getByName(args[++i]); 43 sender_port=Integer.parseInt(args[++i]); 44 continue; 45 } 46 catch(Exception e) { 47 System.err.println(e); 48 help(); 49 return; 50 } 51 } 52 if("-receiver".equals(val)) { 53 receiver=true; 54 try { 55 receiver_addr=InetAddress.getByName(args[++i]); 56 receiver_port=Integer.parseInt(args[++i]); 57 } 58 catch(Exception e) { 59 System.err.println(e); 60 help(); 61 return; 62 } 63 continue; 64 } 65 help(); 66 } 67 68 69 try { 70 if(receiver) { 71 sock=new DatagramSocket (receiver_port, receiver_addr); 72 System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort()); 73 System.out.println("starting as receiver"); 74 receiverLoop(sock); 75 } 76 else { 77 if(sender_addr != null && sender_port > 0) 78 sock=new DatagramSocket (sender_port, sender_addr); 79 else 80 sock=new DatagramSocket (); 81 System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort()); 82 System.out.println("starting as sender"); 83 senderLoop(sock); 84 } 85 } 86 catch(Exception e) { 87 System.err.println(e); 88 } 89 90 } 91 92 93 static void receiverLoop(DatagramSocket sock) { 94 DatagramPacket packet; 95 byte[] buf; 96 97 while(true) { 98 buf=new byte[256]; 99 packet=new DatagramPacket (buf, buf.length); 100 try { 101 sock.receive(packet); 102 System.out.println("Receive packet from " + packet.getAddress().getHostAddress() + 103 ':' + packet.getPort() + ": " + new String (packet.getData())); 104 105 buf="ACK".getBytes(); 106 packet=new DatagramPacket (buf, buf.length, packet.getAddress(), packet.getPort()); 107 sock.send(packet); 108 } 109 catch(Exception e) { 110 System.err.println(e); 111 break; 112 } 113 } 114 } 115 116 117 118 static void senderLoop(DatagramSocket sock) { 119 DataInputStream in; 120 String line; 121 DatagramPacket packet; 122 InetAddress dst; 123 int dst_port; 124 StringTokenizer tok; 125 StringBuffer sb; 126 String tmp, buf; 127 byte[] bbuf; 128 AckReceiver ack_receiver=new AckReceiver(sock); 129 130 ack_receiver.start(); 131 in=new DataInputStream (System.in); 132 while(true) { 133 try { 134 System.out.print("> "); 135 line=in.readLine(); 136 if(line.startsWith("quit") || line.startsWith("exit")) 137 break; 138 tok=new StringTokenizer (line); 139 try { 140 dst=InetAddress.getByName(tok.nextToken()); 141 dst_port=Integer.parseInt(tok.nextToken()); 142 } 143 catch(Exception e) { 144 System.err.println(e); 145 continue; 146 } 147 sb=new StringBuffer (); 148 while(tok.hasMoreTokens()) { 149 tmp=tok.nextToken(); 150 sb.append(tmp + ' '); 151 } 152 buf=sb.toString(); 153 System.out.println("sending " + buf); 154 bbuf=buf.getBytes(); 155 packet=new DatagramPacket (bbuf, bbuf.length, dst, dst_port); 156 sock.send(packet); 157 } 158 catch(Exception e) { 159 System.err.println(e); 160 break; 161 } 162 } 163 ack_receiver.stop(); 164 } 165 166 167 private static class AckReceiver implements Runnable { 168 DatagramSocket sock; 169 DatagramPacket packet; 170 byte[] buf; 171 Thread t=null; 172 173 AckReceiver(DatagramSocket sock) { 174 this.sock=sock; 175 } 176 177 public void run() { 178 while(t != null) { 179 try { 180 buf=new byte[256]; 181 packet=new DatagramPacket (buf, buf.length); 182 sock.receive(packet); 183 System.out.println("<< Received packet from " + 184 packet.getAddress().getHostAddress() + ':' + 185 packet.getPort() + ": " + new String (packet.getData())); 186 } 187 catch(Exception e) { 188 System.err.println(e); 189 break; 190 } 191 } 192 t=null; 193 } 194 195 void start() { 196 t=new Thread (this, "UcastTest.AckReceiver thread"); 197 t.start(); 198 } 199 200 void stop() { 201 if(t != null && t.isAlive()) { 202 t=null; 203 try { 204 sock.close(); 205 } 206 catch(Exception e) { 207 } 208 } 209 } 210 } 211 212 213 static void help() { 214 System.out.println("UcastTest [-help] [-sender | -receiver <local addr> <local port>] " + 215 "[-sender_addr <sender's bind address> <sender' port>]"); 216 } 217 218 219 } 220 | Popular Tags |