01: package org.jgroups.tests;
02:
03: import java.net.*;
04:
05: /**
06: * @author Bela Ban Dec 19
07: * @author 2003
08: * @version $Id: McastLoopbackTest.java,v 1.3 2005/06/23 13:31:10 belaban Exp $
09: */
10: public class McastLoopbackTest {
11:
12: public static void main(String[] args) {
13: byte[] recv_buf = new byte[1024], send_buf = "Bela Ban"
14: .getBytes();
15: MulticastSocket mcast_sock;
16: String group_name = "230.1.2.3";
17: int mcast_port = 7500;
18: SocketAddress mcast_addr, local_addr;
19: NetworkInterface bind_interface;
20: DatagramPacket send_packet, recv_packet;
21:
22: if (args.length != 1) {
23: System.out.println("McastTest <bind interface>");
24: return;
25: }
26:
27: try {
28: bind_interface = NetworkInterface
29: .getByInetAddress(InetAddress.getByName(args[0]));
30: if (bind_interface == null) {
31: System.err.println("bind interface " + args[0]
32: + " not found");
33: return;
34: }
35:
36: local_addr = new InetSocketAddress(args[0], 0);
37: System.out.println("local_addr=" + local_addr);
38:
39: mcast_addr = new InetSocketAddress(InetAddress
40: .getByName(group_name), mcast_port);
41:
42: mcast_sock = new MulticastSocket(local_addr);
43:
44: local_addr = mcast_sock.getLocalSocketAddress();
45: mcast_sock.setTimeToLive(32);
46: // mcast_sock.setLoopbackMode(false);
47:
48: System.out.println("mcast_sock: local addr="
49: + mcast_sock.getLocalSocketAddress()
50: + ", interface=" + mcast_sock.getInterface());
51:
52: mcast_sock.setInterface(InetAddress.getByName(args[0]));
53: mcast_sock.setNetworkInterface(bind_interface);
54: System.out.println("mcast_sock: local addr="
55: + mcast_sock.getLocalSocketAddress()
56: + ", interface=" + mcast_sock.getInterface());
57:
58: System.out.println("-- joining " + mcast_addr + " on "
59: + bind_interface);
60: mcast_sock.joinGroup(mcast_addr, bind_interface);
61:
62: System.out.println("mcast_sock: local addr="
63: + mcast_sock.getLocalSocketAddress()
64: + ", interface=" + mcast_sock.getInterface());
65:
66: send_packet = new DatagramPacket(send_buf, send_buf.length,
67: mcast_addr);
68: recv_packet = new DatagramPacket(recv_buf, recv_buf.length);
69:
70: mcast_sock.send(send_packet);
71: mcast_sock.receive(recv_packet);
72: System.out.println("-- received "
73: + new String(recv_packet.getData(), 0, 8)
74: + " from " + recv_packet.getSocketAddress());
75: } catch (Exception e) {
76: e.printStackTrace();
77: }
78: }
79: }
|