Code example for MulticastSocket

Methods: joinGroup, setSoTimeout, sleep

0
        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);
		} 
	} 
 
	/** 
	 * @tests java.net.MulticastSocket#MulticastSocket() 
	 */ 
	public void test_Constructor() throws IOException { 
		// regression test for 497 
        MulticastSocket s = new MulticastSocket();
        // regression test for Harmony-1162 
        assertTrue(s.getReuseAddress());
	} 
 
	/** 
	 * @tests java.net.MulticastSocket#MulticastSocket(int) 
	 */ 
	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();
	} 
 
	/** 
	 * @tests java.net.MulticastSocket#getInterface() 
	 */ 
	public void test_getInterface() throws Exception { 
		// Test for method java.net.InetAddress 
		// java.net.MulticastSocket.getInterface() 
		assertTrue("Used for testing.", true); 
 
		int groupPort = Support_PortManager.getNextPortForUDP();
 
                if (atLeastOneInterface) {
                        // validate that we get the expected response when one was not 
                        // set 
                        mss = new MulticastSocket(groupPort);
                        String preferIPv4StackValue = System
                                        .getProperty("java.net.preferIPv4Stack");
                        String preferIPv6AddressesValue = System
                                        .getProperty("java.net.preferIPv6Addresses");
                        if (((preferIPv4StackValue == null) || preferIPv4StackValue
                                        .equalsIgnoreCase("false"))
                                        && (preferIPv6AddressesValue != null)
                                        && (preferIPv6AddressesValue.equals("true"))) {
                                // we expect an IPv6 ANY in this case 
                                assertEquals("inet Address returned when not set", 
                                             InetAddress.getByName("::0"),
                                             mss.getInterface());
                        } else { 
                                // we expect an IPv4 ANY in this case 
                                assertEquals("inet Address returned when not set", 
                                             InetAddress.getByName("0.0.0.0"),
                                             mss.getInterface());
                        } 
 
                        // 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()));
                        } 
 
                } 
	} 
 
	/** 
	 * @throws IOException 
	 * @tests java.net.MulticastSocket#getNetworkInterface() 
	 */ 
	public void test_getNetworkInterface() throws IOException { 
        int groupPort = Support_PortManager.getNextPortForUDP();
        if (atLeastOneInterface) {
            // validate that we get the expected response when one was not 
            // set 
            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 
            String preferIPv4StackValue = System
                    .getProperty("java.net.preferIPv4Stack");
            String preferIPv6AddressesValue = System
                    .getProperty("java.net.preferIPv6Addresses");
            if (((preferIPv4StackValue == null) || preferIPv4StackValue
                    .equalsIgnoreCase("false"))
                    && (preferIPv6AddressesValue != null)
                    && (preferIPv6AddressesValue.equals("true"))) {
                assertEquals("network interface returned wrong network interface when not set:" 
                             + theInterface,
                             firstAddress, InetAddress.getByName("::0"));
 
            } else { 
                assertEquals("network interface returned wrong network interface when not set:" 
                             + theInterface,
                             InetAddress.getByName("0.0.0.0"),
                             firstAddress);
            } 
 
            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());
            } 
 
            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());
            } 
        } 
    } 
 
	/** 
	 * @tests java.net.MulticastSocket#getTimeToLive() 
	 */ 
	public void test_getTimeToLive() { 
		try { 
			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());
			ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST); 
		} catch (Exception e) {
			handleException(e, SO_MULTICAST);
		} 
	} 
 
	/** 
	 * @tests java.net.MulticastSocket#getTTL() 
	 */ 
	public void test_getTTL() { 
		// Test for method byte java.net.MulticastSocket.getTTL() 
 
		try { 
			mss = new MulticastSocket();
			mss.setTTL((byte) 120);
			assertEquals("Returned incorrect TTL", 
                                     120, mss.getTTL());
			ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST); 
		} catch (Exception e) {
			handleException(e, SO_MULTICAST);
		} 
	} 
 
	/** 
	 * @tests java.net.MulticastSocket#joinGroup(java.net.InetAddress) 
	 */ 
	public void test_joinGroupLjava_net_InetAddress() throws Exception { 
		// Test for method void 
		// java.net.MulticastSocket.joinGroup(java.net.InetAddress) 
                String msg = null;
		InetAddress group = null;
		int[] ports = Support_PortManager.getNextPortsForUDP(2);
		int groupPort = ports[0];
                group = InetAddress.getByName("224.0.0.3");
                server = new MulticastServer(group, groupPort);
                server.start();
                Thread.sleep(1000);
                msg = "Hello World";
                mss = new MulticastSocket(ports[1]);
                DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
                                .length(), group, groupPort);
                mss.send(sdp, (byte) 10);
                Thread.sleep(1000);
 
                assertEquals("Group member did not recv data", 
                             msg,
                             new String(server.rdp.getData(), 0, server.rdp.getLength()));
	}