Code example for DatagramChannel

Methods: connectreadsocketisBlocking

0
 
        // bind and connect 
 
        this.channel1.socket().bind(localAddr2);
        this.channel1.connect(localAddr1);
        this.channel2.socket().bind(localAddr1);
        this.channel2.connect(localAddr2);
 
        // write 
        ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray);
        assertEquals(0, this.channel1.write(sourceBuf));
 
        // read 
        ByteBuffer targetBuf = ByteBuffer.wrap(targetArray);
        // empty message let the reader blocked 
        new Thread() {
            public void run() { 
                try { 
                    Thread.sleep(TIME_UNIT);
                    channel2.configureBlocking(false);
                    Thread.sleep(TIME_UNIT * 5);
                    channel2.close();
                } catch (Exception e) {
                    // do nothing 
                } 
            } 
        }.start();
        try { 
            assertTrue(this.channel2.isBlocking());
            this.channel2.read(targetBuf);
            fail("Should throw AsynchronousCloseException");
        } catch (AsynchronousCloseException e) {
            assertFalse(this.channel2.isBlocking());
            // OK. 
        } 
    } 
 
    public void testReadWrite_Block_8KB() throws Exception { 
Contextual code suggestions in your IDE  Get Codota for Java