Code example for SocketChannel

Methods: connectfinishConnectisConnectedisConnectionPendingcloseisOpenconfigureBlocking

0
        this.channel1.close();
        assertFalse(this.channel1.isOpen());
    } 
 
    public void testIsConnected() throws Exception {
        assertFalse(this.channel1.isConnected());// not connected
        this.channel1.configureBlocking(false);
        assertFalse(this.channel1.connect(localAddr1));
        assertFalse(this.channel1.isConnected());
        assertTrue(this.channel1.isConnectionPending());
        assertTrue(tryFinish());
        assertTrue(this.channel1.isConnected());
        this.channel1.close();
        assertFalse(this.channel1.isConnected());
    } 
 
    public void testIsConnectionPending() throws Exception {
        // ensure 
        ensureServerClosed(); 
        this.channel1.configureBlocking(false);
        assertFalse(this.channel1.isConnectionPending());
        // finish 
        try { 
            this.channel1.finishConnect();
            fail("Should throw NoConnectionPendingException");
        } catch (NoConnectionPendingException e) {
            // OK. 
        } 
        assertFalse(this.channel1.isConnectionPending());
        // connect 
        assertFalse(this.channel1.connect(localAddr1));
        assertTrue(this.channel1.isConnectionPending());
        this.channel1.close();
 
        assertFalse(this.channel1.isConnectionPending());
    } 
 
    public void testChannelBasicStatus() { 
        Socket gotSocket = this.channel1.socket();
        assertFalse(gotSocket.isClosed());
        assertTrue(this.channel1.isBlocking());
        assertFalse(this.channel1.isRegistered());
        assertEquals((SelectionKey.OP_CONNECT | SelectionKey.OP_READ |
                SelectionKey.OP_WRITE), this.channel1.validOps());
        assertEquals(SelectorProvider.provider(), this.channel1.provider());
    } 
 
    public void testOpenSocketAddress() throws IOException {
        this.channel1 = SocketChannel.open(localAddr1);
        assertTrue(this.channel1.isConnected());
 
        SocketAddress newTypeAddress = new SubSocketAddress();
        try { 
            this.channel1 = SocketChannel.open(newTypeAddress);
            fail("Should throw UnexpectedAddressTypeException");
        } catch (UnsupportedAddressTypeException e) {
            // expected 
        } 
 
        SocketAddress unresolvedAddress =
                InetSocketAddress.createUnresolved("127.0.0.1", 8080);
        try { 
            this.channel1 = SocketChannel.open(unresolvedAddress);
            fail("Should throw UnresolvedAddressException");
        } catch (UnresolvedAddressException e) {
            // expected 
        } 
 
        SocketChannel channel1IP = null;
        try { 
            channel1IP = SocketChannel.open(null);
            fail("Should throw an IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // correct 
        } 
        assertNull(channel1IP);
    } 
 
    private void ensureServerClosed() throws IOException {
        if (null != this.server1) {
            this.server1.close();
            assertTrue(this.server1.isClosed());
        } 
    } 
 
    private void statusConnected_NotPending() { 
        assertTrue(this.channel1.isConnected());
        assertFalse(this.channel1.isConnectionPending());
        assertTrue(this.channel1.isOpen());
    } 
 
    private boolean tryFinish() throws IOException {
        /* 
         * the result of finish will be asserted in multi-thread tests. 
         */ 
        boolean connected = false;
        assertTrue(this.channel1.isOpen());
        try { 
            connected = this.channel1.finishConnect();
        } catch (SocketException e) {
            // Finish connection failed, probably due to reset by peer error. 
        } 
        if (connected) {
            statusConnected_NotPending();