そこで、Javaでチャットサーバーを作成して、Minecraftサーバーでのホスティングと通信をHamachiに依存しないようにしています。サーバーにコマンドを追加する方法がわからないということを除けば、完全に機能します。私のメインループは次のようになります。
/*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()
)。これを回避するにはどうすればよいですか?