В многопоточном сервере чата Java я хочу, чтобы клиент отправлял пользователю сообщение с приглашением отправить сообщение всей группе чата, но приглашение сообщения также появляется, когда клиент получает сообщения от другие клиенты.
![enter image description here](https://i.stack.imgur.com/aPFT2.png)
Я решил создать два потока внутри клиента, один для отправки сообщений, а другой для получения сообщений. Вот основной метод клиента.
public static void main(String args[]) throws UnknownHostException, IOException
{
System.out.print("Please enter your username: ");
Scanner scn = new Scanner(System.in);
// getting localhost ip
InetAddress ip = InetAddress.getByName("localhost");
// establish the connection
Socket s = new Socket(ip, ServerPort);
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// sendMessage thread
Thread sendMessage = new Thread(new Runnable()
{
@Override
public void run() {
while (true) {
// read the message to deliver.
String msg = scn.nextLine();
if(msg.equals(".")){
isLoggedIn = false;
} else {
isLoggedIn = true;
}
try {
// write on the output stream
dos.writeUTF(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
// readMessage thread
Thread readMessage = new Thread(new Runnable()
{
@Override
public void run() {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
while (true) {
try {
if(isLoggedIn == true){
System.out.print("Enter a message: ");
}
// read the message sent to this client
String msg = dis.readUTF();
System.out.println("["+ timestamp + "] " + msg);
} catch (IOException e) {
//e.printStackTrace();
System.exit(0);
}
}
}
});
sendMessage.start();
readMessage.start();
}
Теперь я хочу, чтобы клиент отображал подсказку «Введите сообщение» только тогда, когда пользователь вводит сообщение, а не когда другой клиент или сервер отправка сообщения пользователю. Я не знаю, связаны ли эти проблемы с этими двумя потоками или нет. Если вы знаете, в чем проблема, дайте мне знать.