我試圖去掌握與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。 –