Code example for DatagramSocket

Methods: bind, close, getLocalSocketAddress

0
        theSocket.close();
    } 
 
    public void test_getLocalSocketAddress_late_bind() throws Exception { 
        // An unbound socket should return null as its local address. 
        DatagramSocket theSocket = new DatagramSocket((SocketAddress) null);
        assertNull(theSocket.getLocalSocketAddress());
 
        // now bind the socket and make sure we get the right answer 
        int portNumber = Support_PortManager.getNextPortForUDP();
        InetSocketAddress localAddress = new InetSocketAddress(InetAddress.getLocalHost(), portNumber);
        theSocket.bind(localAddress);
        assertEquals(localAddress, theSocket.getLocalSocketAddress());
        theSocket.close();
    } 
 
    public void test_getLocalSocketAddress_unbound() throws Exception { 
        int portNumber = Support_PortManager.getNextPortForUDP();
        InetSocketAddress localAddress1 = new InetSocketAddress(InetAddress.getLocalHost(), portNumber);
        DatagramSocket s = new DatagramSocket(localAddress1);
        assertEquals(localAddress1, s.getLocalSocketAddress());