Code example for DatagramSocket

Methods: connect, getPort

0
    } 
 
    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 
            theSocket.connect(InetAddress.getLocalHost(), portNumber);
            assertTrue("getPort returned wrong value:" + theSocket.getPort()
                    + ":Expected:" + portNumber,
                    theSocket.getPort() == portNumber);
        } catch (Exception e) {
            fail("unexpected exception during getPort test : " + e.getMessage());
        } 
    } 
 
    public void test_getReceiveBufferSize() throws Exception { 
        int portNumber = Support_PortManager.getNextPortForUDP();