クライアントからメッセージを受け取る単純なサーバー用のコードを作成しようとしています。接続は成功ですが、サーバーがクライアントに接続すると、メッセージは画面に表示されません。クライアント側からの接続を閉じると、サーバーはすべてのメッセージを一度に受信します。Javaソケットサーバーは、接続が終了した後に受信したメッセージのみを表示します。
各行は個別にリアルタイムで処理する必要があるため、メッセージを1行ずつ受け取る必要があります(私は運転手の認識にHMMを使用しています)。
誰かが私に間違っていることを教えてもらえますか?
ありがとうございます。
public static void main(String args[]) {
ServerSocket my_server = null;
String received_message;
DataOutputStream output = null;
Socket socket_connected = null;
try {
my_server = new ServerSocket(9090);
}
catch (IOException excepcion) {
System.out.println(excepcion);
}
try {
socket_connected = my_server.accept();
BufferedReader entrada = null;
while (socket_connected.isConnected() == true){
entrada = new BufferedReader(new InputStreamReader(socket_connected.getInputStream()));
output = new DataOutputStream(socket_connected.getOutputStream());
System.out.println("Confirmando conexion al cliente....");
received_message = entrada.readLine();
System.out.println(received_message);
entrada.close();
output.close();
}
socket_connected.close();
}
catch (IOException excepcion) {
System.out.println(excepcion);
}
}
'flush'メソッドを知っていますか?ソケット(クライアント側またはサーバー側)に書き込んだ後で使用することができます –