提问人:Rahul 提问时间:8/29/2023 最后编辑:Mark RotteveelRahul 更新时间:8/29/2023 访问量:28
serverSocketChannel.accept() 返回 null
serverSocketChannel.accept() returns null
问:
我正在尝试用Java NIO编写一个简单的客户端服务器程序。
代码如下:
package org.lb;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Set;
public class Test {
public static void main(String[] args) throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(8080));
serverSocketChannel.configureBlocking(false);
Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
selector.select();
Set<SelectionKey> selectionKeySet = selector.selectedKeys();
for (SelectionKey selectionKey : selectionKeySet) {
if (selectionKey.isAcceptable()) {
SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel == null) {
System.out.println("No connection...");
} else {
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
System.out.println("Connection done!");
}
}
if (selectionKey.isReadable()) {
System.out.println("isReadable");
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
socketChannel.close();
}
}
}
}
}
您可以看到,我只在返回 true 时调用。因此,我认为它应该始终返回一个有效的实例,但有时它也会返回 null。serverSocketChannel.accept()
selectionKey.isAcceptable()
serverSocketChannel.accept()
SocketChannel
我正在尝试使用 Postman 发送请求,但浏览器请求也是如此。
使用 Postman 单个请求输出
Connection done!
No connection...
isReadable
Connection done!
No connection...
isReadable
我以为它打印了一次,但它打印了两次。另外,为什么要打印?Connection done!
No connection...
使用 curl single 请求输出
Connection done!
isReadable
No connection...
答: 暂无答案
评论
null
”isAcceptable
isAcceptable
serverSocketChannel.accept()
isAcceptable()
selectedKeys()