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行就报错了。。。。。
你应该把连接注册到selector。然后在事件中处理。然后决定是否重试connect