Code example for DatagramChannel

Methods: connectisConnectedwriteconfigureBlocking

0
        ByteBuffer[] writeBuf = new ByteBuffer[2];
        writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        InetSocketAddress ipAddr = localAddr1;
        // non-block mode 
        this.channel1.configureBlocking(false);
        try { 
            this.channel1.write(writeBuf, 0, 2);
            fail("Should throw NotYetConnectedException.");
        } catch (NotYetConnectedException e) {
            // correct 
        } 
        this.channel1.connect(ipAddr);
        assertTrue(this.channel1.isConnected());
        assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2));
        // cannot be buffered again! 
        assertEquals(0, this.channel1.write(writeBuf, 0, 1));
 
    } 
 
    public void testWriteByteBufferArrayIntInt_NoConnectIndexBad() 
            throws IOException { 
Stop searching for code, let great code find you!  Add Codota to your java IDE