当我这样编码时:
ServerSocketChannel ssc = ServerSocketChannel.open();
InetSocketAddress sa = new InetSocketAddress("localhost",8888);
ssc.socket().bind(sa);
ssc.configureBlocking(false);
ssc.socket().accept();
the ServerSocket.accept()
方法抛出java.nio.channels.IllegalBlockingModeException
。为什么我不能打电话accept()
,即使我将阻塞设置为false
?
问题是你正在打电话ssc.socket().accept()
, not ssc.accept()
。如果将最后一行更改为ssc.accept()
那么它将按预期工作,如果有人正在等待,则返回 SocketChannel,否则返回 null。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)