1 3 package org.jgroups.tests; 4 5 6 import java.io.DataInputStream ; 7 import java.net.DatagramPacket ; 8 import java.net.DatagramSocket ; 9 import java.net.InetAddress ; 10 import java.net.MulticastSocket ; 11 12 13 14 15 25 public class McastSenderTest { 26 27 public static void main(String args[]) { 28 MulticastSocket sock; 29 InetAddress mcast_addr=null, bind_addr=null; 30 DatagramPacket packet; 31 byte[] buf=new byte[0]; 32 String tmp; 33 int ttl=32; 34 String line; 35 DataInputStream 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 ex) { 70 System.err.println(ex); 71 return; 72 } 73 74 75 try { 76 sock=new MulticastSocket (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 (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 (buf, buf.length, mcast_addr, port); 97 sock.send(packet); 98 } 99 } 100 catch(Exception 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 { 114 DatagramSocket sock; 115 DatagramPacket packet; 116 byte[] buf; 117 Thread t=null; 118 119 AckReceiver(DatagramSocket 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 (buf, buf.length); 128 sock.receive(packet); 129 System.out.println("<< Received response from " + 130 packet.getAddress().getHostAddress() + ':' + 131 packet.getPort() + ": " + new String (packet.getData())); 132 } 133 catch(Exception e) { 134 System.err.println(e); 135 break; 136 } 137 } 138 t=null; 139 } 140 141 void start() { 142 t=new Thread (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 e) { 153 } 154 } 155 } 156 } 157 158 159 } 160 | Popular Tags |