Code example for DatagramSocket

Methods: connect, getPort

0
 
    /** 
     * @tests java.net.DatagramSocket#getPort() 
     */ 
    public void test_getPort() throws IOException { 
        DatagramSocket theSocket = new DatagramSocket();
        assertEquals("Expected -1 for remote port as not connected", -1,
                theSocket.getPort());
 
        // Now connect the socket and validate that we get the right port 
        int portNumber = 49152; // any valid port, even if it is unreachable
        theSocket.connect(InetAddress.getLocalHost(), portNumber);
        assertEquals("getPort returned wrong value", portNumber, theSocket
                .getPort());
    } 
 
    public void test_getReceiveBufferSize() throws Exception { 
        DatagramSocket ds = new DatagramSocket();
        ds.setReceiveBufferSize(130);
        assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130);
    }