@Override public void onConnect(SelectionKey key) throws IOException { key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT); ConnectFuture future = (ConnectFuture) key.attachment(); if (future == null || future.isCancelled()) { key.channel().close(); key.cancel(); return; } try { if (!((SocketChannel) key.channel()).finishConnect()) { future.failure( new IOException( "Connect to " + SystemUtils.getRawAddress( future.getInetSocketAddressWrapper().getInetSocketAddress()) + ":" + future.getInetSocketAddressWrapper().getInetSocketAddress().getPort() + " fail")); } else { key.attach(null); this.addSession( this.createSession( (SocketChannel) key.channel(), future.getInetSocketAddressWrapper())); future.setResult(Boolean.TRUE); } } catch (Exception e) { future.failure(e); key.cancel(); throw new IOException( "Connect to " + SystemUtils.getRawAddress( future.getInetSocketAddressWrapper().getInetSocketAddress()) + ":" + future.getInetSocketAddressWrapper().getInetSocketAddress().getPort() + " fail," + e.getMessage()); } }