Code example for DatagramSocket

Methods: getLocalPort

0
 
    public void test_getLocalPort() { 
        // Test for method int java.net.DatagramSocket.getLocalPort() 
        try { 
            int portNumber = Support_PortManager.getNextPortForUDP();
            ds = new java.net.DatagramSocket(portNumber);
            assertTrue("Returned incorrect port",
                    ds.getLocalPort() == portNumber);
        } catch (Exception e) {
            fail("Exception during getLocalAddress : " + e.getMessage());
        } 
    } 
 
    public void test_getPort() { 
        try { 
            int portNumber = Support_PortManager.getNextPortForUDP();
            DatagramSocket theSocket = new DatagramSocket(portNumber);
            assertEquals("Expected -1 for remote port as not connected",
                    -1, theSocket.getPort());
 
            // now connect the socket and validate that we get the right port