1 3 package org.jgroups.tests; 4 5 import java.net.DatagramPacket ; 6 import java.net.InetAddress ; 7 import java.net.MulticastSocket ; 8 import java.net.NetworkInterface ; 9 import java.util.Enumeration ; 10 11 12 13 24 public class McastReceiverTest1_4 { 25 26 public static void main(String args[]) { 27 InetAddress mcast_addr=null, bind_addr=null; 28 String tmp; 29 int port=5555; 30 boolean use_all_interfaces=false; 31 32 try { 33 for(int i=0; i < args.length; i++) { 34 tmp=args[i]; 35 if("-help".equals(tmp)) { 36 help(); 37 return; 38 } 39 if("-bind_addr".equals(tmp)) { 40 bind_addr=InetAddress.getByName(args[++i]); 41 continue; 42 } 43 if("-mcast_addr".equals(tmp)) { 44 mcast_addr=InetAddress.getByName(args[++i]); 45 continue; 46 } 47 if("-port".equals(tmp)) { 48 port=Integer.parseInt(args[++i]); 49 continue; 50 } 51 if("-use_all_interfaces".equals(tmp)) { 52 use_all_interfaces=true; 53 continue; 54 } 55 help(); 56 return; 57 } 58 if(mcast_addr == null) 59 mcast_addr=InetAddress.getByName("224.0.0.150"); 60 } 61 catch(Exception ex) { 62 System.err.println(ex); 63 return; 64 } 65 66 if(use_all_interfaces) { 67 if(!is1_4()) { 68 System.err.println("-use_all_interfaces flag requires JDK 1.4 or greater"); 69 return; 70 } 71 } 72 73 74 try { 75 if(use_all_interfaces) { 76 for(Enumeration en=NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { 77 NetworkInterface intf=(NetworkInterface )en.nextElement(); 78 for(Enumeration e2=intf.getInetAddresses(); e2.hasMoreElements();) { 79 bind_addr=(InetAddress )e2.nextElement(); 80 Receiver r=new Receiver(mcast_addr, bind_addr, port); 82 r.start(); 83 } 84 } 85 } 86 else { 87 Receiver r=new Receiver(mcast_addr, bind_addr, port); 88 r.start(); 89 } 90 } 91 catch(Exception e) { 92 System.err.println(e); 93 } 94 95 } 96 97 98 static boolean is1_4() { 99 Class cl; 100 101 try { 102 cl=Thread.currentThread().getContextClassLoader().loadClass("java.net.NetworkInterface"); 103 return true; 104 } 105 catch(Throwable ex) { 106 return false; 107 } 108 } 109 110 111 static void help() { 112 System.out.println("McastSenderTest [-bind_addr <bind address>] [-help] [-mcast_addr <multicast address>] " + 113 "[-port <port for multicast socket>] [-use_all_interfaces (JDK 1.4 only)]"); 114 } 115 116 117 118 static class Receiver extends Thread { 119 MulticastSocket sock=null; 120 DatagramPacket packet; 121 byte buf[]=null; 122 byte[] recv_buf; 123 124 Receiver(InetAddress mcast_addr, InetAddress bind_interface, int port) throws Exception { 125 sock=new MulticastSocket (port); 126 if(bind_interface != null) 127 sock.setInterface(bind_interface); 128 sock.joinGroup(mcast_addr); 129 System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort() + ", bind interface=" + 130 sock.getInterface()); 131 } 132 133 134 public void run() { 135 while(true) { 136 try { 137 buf=new byte[256]; 138 packet=new DatagramPacket (buf, buf.length); 139 sock.receive(packet); 140 recv_buf=packet.getData(); 141 System.out.println(new String (recv_buf) + " [sender=" + packet.getAddress().getHostAddress() + 142 ':' + packet.getPort() + ']'); 143 } 144 catch(Exception ex) { 145 System.err.println("Receiver terminated: " + ex); 146 break; 147 } 148 } 149 } 150 } 151 152 153 } 154 | Popular Tags |