1 package org.jgroups.tests; 2 3 import org.jgroups.util.Util; 4 5 import java.net.DatagramPacket ; 6 import java.net.DatagramSocket ; 7 import java.net.InetAddress ; 8 import java.net.MulticastSocket ; 9 10 17 public class Probe { 18 MulticastSocket mcast_sock; 19 20 21 public Probe() { 22 23 } 24 25 public void start(InetAddress addr, InetAddress bind_addr, int port, int ttl, final long timeout) throws Exception { 26 mcast_sock=new MulticastSocket (); 27 mcast_sock.setTimeToLive(ttl); 28 if(bind_addr != null) 29 mcast_sock.setInterface(bind_addr); 30 31 byte[] probe_buf=new byte[]{'d', 'i', 'a', 'g'}; 32 DatagramPacket probe=new DatagramPacket (probe_buf, 0, probe_buf.length, addr, port); 33 mcast_sock.send(probe); 34 System.out.println("\n-- send probe on " + addr + ':' + port + '\n'); 35 int i=0; 36 37 new Thread () { 38 public void run() { 39 Util.sleep(timeout); 40 mcast_sock.close(); 41 } 42 }.start(); 43 44 for(;;) { 45 byte[] buf=new byte[65000]; 46 DatagramPacket rsp=new DatagramPacket (buf, 0, buf.length); 47 try { 48 mcast_sock.receive(rsp); 49 } 50 catch(Throwable t) { 51 System.out.println("\n"); 52 return; 53 } 54 byte[] data=rsp.getData(); 55 System.out.println("\n#" + ++i + ": " + new String (data, 0, rsp.getLength())); 56 } 57 } 58 59 60 public static void main(String [] args) { 61 InetAddress addr=null, bind_addr=null; 62 int port=0; 63 int ttl=32; 64 long timeout=10000; 65 66 try { 67 for(int i=0; i < args.length; i++) { 68 if("-addr".equals(args[i])) { 69 addr=InetAddress.getByName(args[++i]); 70 continue; 71 } 72 if("-bind_addr".equals(args[i])) { 73 bind_addr=InetAddress.getByName(args[++i]); 74 continue; 75 } 76 if("-port".equals(args[i])) { 77 port=Integer.parseInt(args[++i]); 78 continue; 79 } 80 if("-ttl".equals(args[i])) { 81 ttl=Integer.parseInt(args[++i]); 82 continue; 83 } 84 if("-timeout".equals(args[i])) { 85 timeout=Long.parseLong(args[++i]); 86 continue; 87 } 88 89 help(); 90 return; 91 } 92 Probe p=new Probe(); 93 p.start(addr, bind_addr, port, ttl, timeout); 94 } 95 catch(Throwable t) { 96 t.printStackTrace(); 97 } 98 } 99 100 static void help() { 101 System.out.println("Probe [-help] [-addr <addr>] [-bind_addr <addr>] [-port <port>] [-ttl <ttl>] [-timeout <timeout>]"); 102 } 103 } 104 | Popular Tags |