Code example for MulticastSocket

Methods: joinGroup, setSoTimeout, sleep

0
                } else if (groupSockAddr != null) {
                    ms.leaveGroup(groupSockAddr, groupNI);
                } 
            } catch (IOException e) {}
        } 
 
        public MulticastServer(InetAddress anAddress, int aPort) throws java.io.IOException {
            rbuf = new byte[512];
            rbuf[0] = -1;
            rdp = new DatagramPacket(rbuf, rbuf.length);
            ms = new MulticastSocket(aPort);
            ms.setSoTimeout(2000);
            groupAddr = anAddress;
            ms.joinGroup(groupAddr);
        } 
 
        public MulticastServer(SocketAddress anAddress, int aPort, NetworkInterface netInterface) throws java.io.IOException {
            rbuf = new byte[512];
            rbuf[0] = -1;
            rdp = new DatagramPacket(rbuf, rbuf.length);
            ms = new MulticastSocket(aPort);
            ms.setSoTimeout(2000);
            groupSockAddr = anAddress;
            groupNI = netInterface;
            ms.joinGroup(groupSockAddr, groupNI);
        } 
    } 
 
    public void test_Constructor() throws IOException { 
        // regression test for 497 
        MulticastSocket s = new MulticastSocket();
        // regression test for Harmony-1162 
        assertTrue(s.getReuseAddress());
    } 
 
    public void test_ConstructorI() throws IOException { 
        MulticastSocket orig = new MulticastSocket();
        int port = orig.getLocalPort();
        orig.close();
        MulticastSocket dup = null;
        try { 
            dup = new MulticastSocket(port);
            // regression test for Harmony-1162 
            assertTrue(dup.getReuseAddress());
        } catch (IOException e) {
            fail("duplicate binding not allowed: " + e);
        } 
        if (dup != null) {
            dup.close();
        } 
    } 
 
    public void test_getInterface() throws Exception { 
        int groupPort = Support_PortManager.getNextPortForUDP();
 
        // validate that we get the expected response when one was not set 
        MulticastSocket mss = new MulticastSocket(groupPort);
        // we expect an ANY address in this case 
        assertTrue(mss.getInterface().isAnyLocalAddress());
 
        // validate that we get the expected response when we set via 
        // setInterface 
        Enumeration addresses = networkInterface1.getInetAddresses();
        if (addresses.hasMoreElements()) {
            InetAddress firstAddress = (InetAddress) addresses.nextElement();
            mss.setInterface(firstAddress);
            assertEquals("getNetworkInterface did not return interface set by setInterface", firstAddress, mss.getInterface());
 
            groupPort = Support_PortManager.getNextPortForUDP();
            mss = new MulticastSocket(groupPort);
            mss.setNetworkInterface(networkInterface1);
            assertEquals("getInterface did not return interface set by setNetworkInterface", networkInterface1, NetworkInterface.getByInetAddress(mss.getInterface()));
        } 
 
        mss.close();
    } 
 
    public void test_getNetworkInterface() throws IOException { 
        int groupPort = Support_PortManager.getNextPortForUDP();
 
        // validate that we get the expected response when one was not set 
        MulticastSocket mss = new MulticastSocket(groupPort);
        NetworkInterface theInterface = mss.getNetworkInterface();
        assertTrue("network interface returned wrong network interface when not set:" + theInterface,
                theInterface.getInetAddresses().hasMoreElements());
        InetAddress firstAddress = (InetAddress) theInterface.getInetAddresses().nextElement();
        // validate we the first address in the network interface is the ANY address 
        assertTrue(firstAddress.isAnyLocalAddress());
 
        mss.setNetworkInterface(networkInterface1);
        assertEquals("getNetworkInterface did not return interface set by setNeworkInterface",
                networkInterface1, mss.getNetworkInterface());
 
        if (atLeastTwoInterfaces) {
            mss.setNetworkInterface(networkInterface2);
            assertEquals("getNetworkInterface did not return network interface set by second setNetworkInterface call",
                    networkInterface2, mss.getNetworkInterface());
        } 
        mss.close();
 
        groupPort = Support_PortManager.getNextPortForUDP();
        mss = new MulticastSocket(groupPort);
        if (IPV6networkInterface1 != null) {
            mss.setNetworkInterface(IPV6networkInterface1);
            assertEquals("getNetworkInterface did not return interface set by setNeworkInterface",
                    IPV6networkInterface1, mss.getNetworkInterface());
        } 
 
        // validate that we get the expected response when we set via setInterface 
        groupPort = Support_PortManager.getNextPortForUDP();
        mss = new MulticastSocket(groupPort);
        Enumeration addresses = networkInterface1.getInetAddresses();
        if (addresses.hasMoreElements()) {
            firstAddress = (InetAddress) addresses.nextElement();
            mss.setInterface(firstAddress);
            assertEquals("getNetworkInterface did not return interface set by setInterface",
                    networkInterface1, mss.getNetworkInterface());
        } 
        mss.close();
    } 
 
    public void test_getTimeToLive() throws Exception { 
        MulticastSocket mss = new MulticastSocket();
        mss.setTimeToLive(120);
        assertEquals("Returned incorrect 1st TTL", 120, mss.getTimeToLive());
        mss.setTimeToLive(220);
        assertEquals("Returned incorrect 2nd TTL", 220, mss.getTimeToLive());
    } 
 
    public void test_getTTL() throws Exception { 
        MulticastSocket mss = new MulticastSocket();
        mss.setTTL((byte) 120);
        assertEquals("Returned incorrect TTL", 120, mss.getTTL());
    } 
 
    public void test_joinGroupLjava_net_InetAddress_IPv4() throws Exception { 
        test_joinGroupLjava_net_InetAddress(GOOD_IPv4);
    } 
 
    public void test_joinGroupLjava_net_InetAddress_IPv6() throws Exception { 
        test_joinGroupLjava_net_InetAddress(GOOD_IPv6);
    } 
 
    private void test_joinGroupLjava_net_InetAddress(InetAddress group) throws Exception {
        int[] ports = Support_PortManager.getNextPortsForUDP(2);
        int groupPort = ports[0];
 
        MulticastServer server = new MulticastServer(group, groupPort);
        server.start();
        Thread.sleep(1000);
        String msg = "Hello World";
        MulticastSocket mss = new MulticastSocket(ports[1]);
        DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg.length(), group, groupPort);
        mss.send(sdp, (byte) 10);
        Thread.sleep(1000);
        String receivedMessage = new String(server.rdp.getData(), 0, server.rdp.getLength());
        assertEquals("Group member did not recv data", msg, receivedMessage);
        mss.close();
        server.stopServer();
    } 
 
    public void test_joinGroup_null_null() throws Exception { 
        MulticastSocket mss = new MulticastSocket(0);
        try { 
            mss.joinGroup(null, null);
            fail();
        } catch (IllegalArgumentException expected) {
        } 
        mss.close();
    } 
 
    public void test_joinGroup_non_multicast_address_IPv4() throws Exception { 
        MulticastSocket mss = new MulticastSocket(0);
        try { 
            mss.joinGroup(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0), null);
            fail();
        } catch (IOException expected) {
        } 
        mss.close();
    } 
 
    public void test_joinGroup_non_multicast_address_IPv6() throws Exception { 
        MulticastSocket mss = new MulticastSocket(0);
        try { 
            mss.joinGroup(new InetSocketAddress(InetAddress.getByName("::1"), 0), null);
            fail();
        } catch (IOException expected) {
        } 
        mss.close();
    } 
 
    public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface_IPv4() throws Exception { 
        test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface(GOOD_IPv4, BAD_IPv4);
    } 
 
    public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface_IPv6() throws Exception { 
        test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface(GOOD_IPv6, BAD_IPv6);
    } 
 
    private void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface(InetAddress group, InetAddress group2) throws Exception {
        int[] ports = Support_PortManager.getNextPortsForUDP(2);
        int groupPort = ports[0];
        int serverPort = ports[1];
 
        SocketAddress groupSockAddr = new InetSocketAddress(group, groupPort);
 
        // Check that we can join a group using a null network interface. 
        MulticastSocket mss = new MulticastSocket(groupPort);
        mss.joinGroup(groupSockAddr, null);
        mss.setTimeToLive(2);
        Thread.sleep(1000);
 
        // set up the server and join the group on networkInterface1 
        MulticastServer server = new MulticastServer(groupSockAddr, serverPort, networkInterface1);
        server.start();
        Thread.sleep(1000);
        String msg = "Hello World";
        DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg.length(), group, serverPort);
        mss.setTimeToLive(2);
        mss.send(sdp);
        Thread.sleep(1000);
        // now validate that we received the data as expected 
        assertEquals("Group member did not recv data", msg, new String(server.rdp.getData(), 0, server.rdp.getLength()));
        server.stopServer();
        mss.close();
 
        // now validate that we handled the case were we join a 
        // different multicast address. 
        // verify we do not receive the data 
        ports = Support_PortManager.getNextPortsForUDP(2);
        serverPort = ports[0];
        server = new MulticastServer(groupSockAddr, serverPort, networkInterface1);
        server.start();
        Thread.sleep(1000);
 
        groupPort = ports[1];
        mss = new MulticastSocket(groupPort);
        mss.setTimeToLive(10);
        msg = "Hello World - Different Group";
        sdp = new DatagramPacket(msg.getBytes(), msg.length(), group2, serverPort);