因此,我正在Java中創建一個聊天服務器,以便根據Hamachi移出我的Minecraft服務器中的主機和通信。它完美的工作,除了一件事:我不知道如何我的生活如何添加命令到服務器。我的主循環去如下:帶命令的Java聊天服務器
/*Executed in constructor*/
public void listen(int port) throws IOException {
//Initialize the ServerSocket
ss = new ServerSocket(port);
System.out.println("Listening on " + InetAddress.getLocalHost() + ":" + ss.getLocalPort());
running = true;
//Keep accepting connections
while (running) {
//Get the incoming connection
Socket s = ss.accept();
System.out.println("Connection from: " + getFullIP(s));
//Create a DataOutputStream for writing data to the other side
DataOutputStream dataOut = new DataOutputStream(s.getOutputStream());
//Save this stream so I don't have to make it again
outputStreams.put(s, dataOut);
//Create a new thread for this connection
new ServerThread(this, s);
if (!running) {
stop();
}
Scanner cmdScanner = new Scanner(System.in);
String command = cmdScanner.next();
processCommand(command);
}
}
這段代碼的結果是,直到客戶端連接到(因爲ss.accept()
)的服務器我不能鍵入的命令。在執行命令之前,客戶端無法連接(cmdScanner.next()
)。我如何解決這個問題?
謝謝!你能給我一個關於這個過程的輪廓,或者指出我有一個好的方向嗎? – mattbdean
哦!我明白你的意思了。非常感謝! – mattbdean