认清InetAddress.getLocalHost()得到的是什么

在NIO中,在bind地址和端口的时候,或许你会用:
Selector selector = Selector.open();
ServerSocketChannel ssc = ServerSocketChannel.open();
System.out.println("localhost:" + InetAddress.getLocalHost().toString());
InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), 9494);
ssc.socket().bind(address);
ssc.configureBlocking(false);
SelectionKey s = ssc.register(selector, SelectionKey.OP_ACCEPT);

其中:
InetAddress.getLocalHost()并不是你想象中的127.0.0.1,或者你可以试试


输入:
localhost:bluepeer-PC/10.0.0.90


所以如果你想用telnet去测试应该是:telnet 10.0.0.90 9494

你可能感兴趣的:(JAVA)