Code example for DatagramSocket

Methods: close, getLocalAddress, getLocalSocketAddress

0
        InetAddress local = InetAddress.getLocalHost();
        ds = new java.net.DatagramSocket(portNumber, local);
        assertEquals(InetAddress.getByName(InetAddress.getLocalHost().getHostName()), ds.getLocalAddress());
 
        // now check behavior when the ANY address is returned 
        DatagramSocket s = new DatagramSocket(0);
        assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), s.getLocalAddress() instanceof Inet6Address);
        s.close();
    } 
 
    /** 
     * @tests java.net.DatagramSocket#getLocalPort() 
     */ 
    public void test_getLocalPort() throws SocketException { 
        DatagramSocket ds = new DatagramSocket();
        assertTrue("Returned incorrect port", ds.getLocalPort() != 0);
    } 
 
    /** 
     * @tests java.net.DatagramSocket#getPort() 
     */