2 qq 26460555 qq_26460555 于 2017.09.06 11:23 提问

NIO SocketChannel连接报错
private static void testSocketChannel() throws IOException{
    SocketChannel socketChannel = SocketChannel.open();
    socketChannel.configureBlocking(false);
    socketChannel.connect(new InetSocketAddress(10086));
    while(!socketChannel.finishConnect()){
        System.out.println("connecting....");
        try {
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.out.println("connect success!!");
}

代码如图所以,设想是在连接成功之前,控制台会一直输出connecting....,但运行时只输出2行就报错了。。。。。

1个回答

oyljerry
oyljerry   Ds   Rxr 2017.09.06 12:02

你应该把连接注册到selector。然后在事件中处理。然后决定是否重试connect

Csdn user default icon
上传中...
上传图片
插入图片