Code example for DatagramSocket

Methods: bind, close, connect, getRemoteSocketAddress

0
                                        .getLocalHost(), sport)));
        s.close();
 
        // now create one that is not connected and validate that we get the 
        // right answer 
        DatagramSocket theSocket = new DatagramSocket(null);
        portNumber = ports[2];
        theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
                portNumber));
        assertNull("Returned incorrect InetSocketAddress -unconnected socket:"
                + "Expected: NULL", theSocket.getRemoteSocketAddress());
 
        // now connect and validate we get the right answer 
        theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(),
                sport));
        assertTrue("Returned incorrect InetSocketAddress(2):"
                + theSocket.getRemoteSocketAddress().toString(),
                theSocket.getRemoteSocketAddress()
                        .equals(
                                new InetSocketAddress(InetAddress
                                        .getLocalHost(), sport)));
        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);