1 3 package org.jgroups.tests; 4 5 6 import java.net.DatagramPacket ; 7 import java.net.InetAddress ; 8 import java.net.MulticastSocket ; 9 10 11 12 13 23 public class McastReceiverTest { 24 25 public static void main(String args[]) { 26 MulticastSocket sock; 27 InetAddress mcast_addr=null, bind_addr=null; 28 DatagramPacket packet; 29 byte buf[]=null; 30 byte[] recv_buf; 31 String tmp; 32 int port=5555; 33 34 try { 35 for(int i=0; i < args.length; i++) { 36 tmp=args[i]; 37 if("-help".equals(tmp)) { 38 help(); 39 return; 40 } 41 if("-bind_addr".equals(tmp)) { 42 bind_addr=InetAddress.getByName(args[++i]); 43 continue; 44 } 45 if("-mcast_addr".equals(tmp)) { 46 mcast_addr=InetAddress.getByName(args[++i]); 47 continue; 48 } 49 if("-port".equals(tmp)) { 50 port=Integer.parseInt(args[++i]); 51 continue; 52 } 53 help(); 54 return; 55 } 56 if(mcast_addr == null) 57 mcast_addr=InetAddress.getByName("224.0.0.150"); 58 } 59 catch(Exception ex) { 60 System.err.println(ex); 61 return; 62 } 63 64 65 try { 66 sock=new MulticastSocket (port); 67 if(bind_addr != null) 68 sock.setInterface(bind_addr); 69 sock.joinGroup(mcast_addr); 70 System.out.println("Socket=" + sock.getLocalAddress() + ':' + sock.getLocalPort() + ", bind interface=" + 71 sock.getInterface()); 72 73 while(true) { 74 buf=new byte[256]; 75 packet=new DatagramPacket (buf, buf.length); 76 sock.receive(packet); 77 recv_buf=packet.getData(); 78 System.out.println(new String (recv_buf) + " [sender=" + packet.getAddress().getHostAddress() + 79 ':' + packet.getPort() + ']'); 80 byte[] buf2="Hello from Bela".getBytes(); 81 DatagramPacket rsp=new DatagramPacket (buf2, buf2.length, packet.getAddress(), packet.getPort()); 82 sock.send(rsp); 83 } 84 85 } 86 catch(Exception e) { 87 System.err.println(e); 88 } 89 90 } 91 92 93 static void help() { 94 System.out.println("McastSenderTest [-bind_addr <bind address>] [-help] [-mcast_addr <multicast address>] " + 95 "[-port <port for multicast socket>]"); 96 } 97 98 99 } 100 | Popular Tags |