Code example for DatagramSocket

Methods: bindclosegetLocalSocketAddress

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);
Stop searching for code, let great code find you!  Add Codota to your java IDE