我正在尝试掌握 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();
}