Code example for DatagramChannel

Methods: configureBlocking, connect, isConnected, isOpen, socket

0
        DatagramSocket s = this.channel1.socket();
        assertSocketActionAfterConnect(s);
    } 
 
    public void testSocket_NonBlock_ActionsAfterConnect() throws IOException { 
        this.channel1.connect(localAddr1);
        this.channel1.configureBlocking(false);
        DatagramSocket s = this.channel1.socket();
        assertSocketActionAfterConnect(s);
    } 
 
    private void assertSocketBeforeConnect(DatagramSocket s)
            throws SocketException { 
        assertFalse(s.isBound());
        assertFalse(s.isClosed());
        assertFalse(s.isConnected());
        assertFalse(s.getBroadcast());
        assertFalse(s.getReuseAddress());
        assertNull(s.getInetAddress());
        assertTrue(s.getLocalAddress().isAnyLocalAddress());
        assertEquals(s.getLocalPort(), 0);
        assertNull(s.getLocalSocketAddress());
        assertEquals(s.getPort(), -1);
        assertTrue(s.getReceiveBufferSize() >= 8192);
        assertNull(s.getRemoteSocketAddress());
        assertFalse(s.getReuseAddress());
        assertTrue(s.getSendBufferSize() >= 8192);
        assertEquals(s.getSoTimeout(), 0);
        assertEquals(s.getTrafficClass(), 0);
    } 
 
    private void assertSocketAfterConnect(DatagramSocket s)
            throws SocketException { 
        assertTrue(s.isBound());
        assertFalse(s.isClosed());
        assertTrue(s.isConnected());
        assertFalse(s.getBroadcast());
        assertFalse(s.getReuseAddress());
        assertSame(s.getInetAddress(), localAddr1.getAddress());
        assertEquals(s.getLocalAddress(), localAddr1.getAddress());
        assertNotNull(s.getLocalSocketAddress());
        assertEquals(s.getPort(), localAddr1.getPort());
        assertTrue(s.getReceiveBufferSize() >= 8192);
        // not same , but equals 
        assertNotSame(s.getRemoteSocketAddress(), (SocketAddress) localAddr1);
        assertEquals(s.getRemoteSocketAddress(), (SocketAddress) localAddr1);
        assertFalse(s.getReuseAddress());
        assertTrue(s.getSendBufferSize() >= 8192);
        assertEquals(s.getSoTimeout(), 0);
        assertEquals(s.getTrafficClass(), 0);
    } 
 
    private void assertSocketActionBeforeConnect(DatagramSocket s)
            throws IOException { 
        s.connect(localAddr2);
        assertFalse(this.channel1.isConnected());
        assertFalse(s.isConnected());
 
        s.disconnect();
        assertFalse(this.channel1.isConnected());
        assertFalse(s.isConnected());
 
        s.close();
        assertTrue(s.isClosed());
        assertFalse(this.channel1.isOpen());
    } 
 
    private void assertSocketActionAfterConnect(DatagramSocket s)
            throws IOException { 
        assertEquals(s.getPort(), localAddr1.getPort());
        s.connect(localAddr2);
        assertTrue(this.channel1.isConnected());
        assertTrue(s.isConnected());
        // not changed 
        assertEquals(s.getPort(), localAddr1.getPort());
 
        s.disconnect();
        assertFalse(this.channel1.isConnected());
        assertFalse(s.isConnected());
 
        s.close();
        assertTrue(s.isClosed());
        assertFalse(this.channel1.isOpen());
    } 
 
    // ------------------------------------------------------------------- 
    // Test for configureBlocking() 
    // -------------------------------------------------------------------