Code example for DatagramChannel

Methods: configureBlocking, connect, isBlocking, isConnected, isOpen, send

0
    } 
 
    private void sendDataNonBlocking(InetSocketAddress addr, ByteBuffer writeBuf)
            throws IOException { 
        InetSocketAddress ipAddr = addr;
        this.channel1.configureBlocking(false);
        assertEquals(CAPACITY_NORMAL, this.channel1.send(writeBuf, ipAddr));
        assertTrue(this.channel1.isOpen());
        assertFalse(this.channel1.isBlocking());
        this.channel1.connect(ipAddr);
        assertTrue(this.channel1.isConnected());
    } 
 
    /* 
     * Test method for 'DatagramChannelImpl.send(ByteBuffer, SocketAddress)' 
     */ 
    public void testSend_NoServerBlockingCommon() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        sendDataBlocking(localAddr1, writeBuf);
    } 
 
    public void testSend_NoServerNonblockingCommon() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        sendDataNonBlocking(localAddr1, writeBuf);
    } 
 
    public void testSend_NoServerTwice() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        sendDataBlocking(localAddr1, writeBuf);
        // can not buffer twice! 
        assertEquals(0, this.channel1.send(writeBuf, localAddr1));
        try { 
            channel1.send(writeBuf, localAddr2);
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // correct 
        } 
    } 
 
    public void testSend_NoServerNonBlockingTwice() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        sendDataNonBlocking(localAddr1, writeBuf);
        // can not buffer twice! 
        assertEquals(0, this.channel1.send(writeBuf, localAddr1));
        try { 
            channel1.send(writeBuf, localAddr2);
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // correct 
        } 
    }