各ユーザの名前を格納する私のシステムのサーバ内にarraylistを作成しました。これはクライアントに入力され、プリントライターを介してサーバーに送信されます。しかし、クライアントがソケットから切断して、入力したユーザー名を削除すると、このリストが更新されます。更新されたユーザー名リストは、既存のクライアントとサーバーの両方に表示する必要があります。クライアントが切断された後、クライアントのユーザー名をサーバーから削除するにはどうすればよいですか?
サーバー:
do
{
//Wait for client.
client = serverSocket.accept();
in = new BufferedReader(
new InputStreamReader(client.getInputStream()));
out = new PrintWriter(
client.getOutputStream(),true);
userNames.add(in.readLine()); //Adds to the userNames array based read in from the client
System.out.println("---User List---");
for(int size = 0; size < userNames.size(); size++)
{
System.out.println(userNames.get(size));
}
out.println("Welcome, the following users are connected...");
out.println(userNames);
handler = new ClientHandler(client);
handler.start();
}while (true);
クライアント:
do
{
System.out.print("Please enter a username:");
Users user = new Users(keyboard.nextLine());
output.println(user.username);
System.out.println(in.readLine() + "\n");
System.out.println(in.readLine());
System.out.print("\nEnter message ('QUIT' to exit): ");
message = keyboard.nextLine();
output.println(message);
if (!message.equals("QUIT"))
{
response = networkInput.nextLine();
System.out.println("\n" + response);
output.println(user.username);
}
}while (!message.equals("QUIT"));
このケースでは、サーバーでソケットクロージャを検出し、反応を追加する必要があります。見てくださいhttp://stackoverflow.com/questions/10240694/java-socket-api-how-to-tell-if-a-connection-has-been-closed –
それは本当に役立つことはありません。私はクライアントが接続を閉じた後、サーバーにある配列リストからユーザー名を削除することを知る必要があります。 **一度に複数のクライアントを接続することができます。 – DaveDavidson