Code example for DatagramSocket

Methods: bind, close, setReuseAddress

0
        } 
 
        // test case were we set it to true 
        InetSocketAddress theAddress = new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager.getNextPortForUDP());
        theSocket1 = new DatagramSocket(null);
        theSocket2 = new DatagramSocket(null);
        theSocket1.setReuseAddress(true);
        theSocket2.setReuseAddress(true);
        theSocket1.bind(theAddress);
        theSocket2.bind(theAddress);
 
        if (theSocket1 != null) {
            theSocket1.close();
        } 
        if (theSocket2 != null) {
            theSocket2.close();
        } 
 
        // test the default case which we expect to be the same on all 
        // platforms 
        try { 
            theAddress = new InetSocketAddress(InetAddress.getLocalHost(),Support_PortManager.getNextPortForUDP());
            theSocket1 = new DatagramSocket(null);
            theSocket2 = new DatagramSocket(null);
            theSocket1.bind(theAddress);
            theSocket2.bind(theAddress);
            fail("No exception when trying to connect to do duplicate socket bind with re-useaddr left as default");
        } catch (BindException expected) {
        } 
        if (theSocket1 != null) {
            theSocket1.close();
        } 
        if (theSocket2 != null) {
            theSocket2.close();
        } 
    } 
 
    public void test_getReuseAddress() throws Exception { 
        DatagramSocket theSocket = new DatagramSocket();