我试图去掌握与java网络并有服务器与客户端交谈。我发现尝试使套接字非阻塞时发生错误。有人可以看看我的代码并尝试找到该错误吗?无法将套接字设置为非阻塞
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4444);
serverSocket.configureBlocking(false);
System.out.println("Server started");
} catch (IOException e) {
System.out.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (listening){
Socket s = serverSocket.accept();
long id = clients_id++;
ServerThread st = new ServerThread(s, id);
addClient(id, st);
st.start();
}
serverSocket.close();
}
我使用阻塞NIO。 –