KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > McastLoopbackTest


1 package org.jgroups.tests;
2
3 import java.net.*;
4
5 /**
6  * @author Bela Ban Dec 19
7  * @author 2003
8  * @version $Id: McastLoopbackTest.java,v 1.3 2005/06/23 13:31:10 belaban Exp $
9  */

10 public class McastLoopbackTest {
11
12     public static void main(String JavaDoc[] args) {
13         byte[] recv_buf=new byte[1024], send_buf="Bela Ban".getBytes();
14         MulticastSocket mcast_sock;
15         String JavaDoc group_name="230.1.2.3";
16         int mcast_port=7500;
17         SocketAddress mcast_addr, local_addr;
18         NetworkInterface bind_interface;
19         DatagramPacket send_packet, recv_packet;
20
21         if(args.length != 1) {
22             System.out.println("McastTest <bind interface>");
23             return;
24         }
25
26         try {
27             bind_interface=NetworkInterface.getByInetAddress(InetAddress.getByName(args[0]));
28             if(bind_interface == null) {
29                 System.err.println("bind interface " + args[0] + " not found");
30                 return;
31             }
32
33             local_addr=new InetSocketAddress(args[0], 0);
34             System.out.println("local_addr=" + local_addr);
35
36             mcast_addr=new InetSocketAddress(InetAddress.getByName(group_name), mcast_port);
37
38             mcast_sock=new MulticastSocket(local_addr);
39
40             local_addr=mcast_sock.getLocalSocketAddress();
41             mcast_sock.setTimeToLive(32);
42             // mcast_sock.setLoopbackMode(false);
43

44             System.out.println("mcast_sock: local addr=" + mcast_sock.getLocalSocketAddress() +
45                     ", interface=" + mcast_sock.getInterface());
46
47             mcast_sock.setInterface(InetAddress.getByName(args[0]));
48             mcast_sock.setNetworkInterface(bind_interface);
49             System.out.println("mcast_sock: local addr=" + mcast_sock.getLocalSocketAddress() +
50                     ", interface=" + mcast_sock.getInterface());
51
52             System.out.println("-- joining " + mcast_addr + " on " + bind_interface);
53             mcast_sock.joinGroup(mcast_addr, bind_interface);
54
55             System.out.println("mcast_sock: local addr=" + mcast_sock.getLocalSocketAddress() +
56                     ", interface=" + mcast_sock.getInterface());
57
58
59             send_packet=new DatagramPacket(send_buf, send_buf.length, mcast_addr);
60             recv_packet=new DatagramPacket(recv_buf, recv_buf.length);
61
62             mcast_sock.send(send_packet);
63             mcast_sock.receive(recv_packet);
64             System.out.println("-- received " + new String JavaDoc(recv_packet.getData(), 0, 8) +
65                     " from " + recv_packet.getSocketAddress());
66         }
67         catch(Exception JavaDoc e) {
68             e.printStackTrace();
69         }
70     }
71 }
72
Popular Tags