Code example for MulticastSocket

Methods: joinGroup, leaveGroup

0
 
                if (atLeastOneInterface) {
 
                        // now test that we can join and leave a group successfully 
                        groupPort = Support_PortManager.getNextPortForUDP();
                        mss = new MulticastSocket(groupPort);
                        groupSockAddr = new InetSocketAddress(group, groupPort);
                        mss.joinGroup(groupSockAddr, null);
                        mss.leaveGroup(groupSockAddr, null);
                        try { 
                                mss.leaveGroup(groupSockAddr, null);
                                fail( 
                                                "Did not get exception when trying to leave group that was allready left"); 
                        } catch (IOException e) {
                        } 
 
                        InetAddress group2 = InetAddress.getByName("224.0.0.4");
                        groupSockAddr2 = new InetSocketAddress(group2, groupPort);
                        mss.joinGroup(groupSockAddr, networkInterface1);
                        try { 
                                mss.leaveGroup(groupSockAddr2, networkInterface1);
                                fail( 
                                                "Did not get exception when trying to leave group that was never joined"); 
                        } catch (IOException e) {
                        } 
 
                        mss.leaveGroup(groupSockAddr, networkInterface1);
                        if (atLeastTwoInterfaces) {
                                mss.joinGroup(groupSockAddr, networkInterface1);
                                try { 
                                        mss.leaveGroup(groupSockAddr, networkInterface2);
                                        fail( 
                                                        "Did not get exception when trying to leave group on wrong interface joined on [" 
                                                                        + networkInterface1
                                                                        + "] left on [" 
                                                                        + networkInterface2 + "]");