您如何不在多聊天服务器中向自己发送消息?

时间:2019-03-11 21:37:33

标签: java

我有一个多客户端聊天服务器,当有人发送消息时,我希望它转到所有其他客户端,但不发送给其他客户端。当前正在将消息发送给所有人,包括发送消息的人吗?可以在下面找到clientThread.java文件的代码。

import java.net.*;
import java.io.*;
import javax.swing.*;

public class ClientThread extends Thread {

    Socket socket;
    String clientName;
    BufferedReader inputStream;
    PrintWriter outputStream;
    JLabel connectionStatusLabel;

    public ClientThread(Socket socket, JLabel connectionStatusLabel) {
        try {
            this.socket = socket;
            this.connectionStatusLabel = connectionStatusLabel;
            inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            outputStream = new PrintWriter(socket.getOutputStream());

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void run() {
        try {

            while (true) {
                String clientMessage = inputStream.readLine();
                if (clientMessage.equals("remove")) {
                    Server.clientsList.remove(this);
                    this.socket.close();
                    for (ClientThread clientThread : Server.clientsList) {
                        clientThread.outputStream.println(clientName+" is disconnected");
                        clientThread.outputStream.flush();
                    }
                } else if (clientMessage.equals("client_name")) {
                    clientName = inputStream.readLine();
                    for (ClientThread clientThread : Server.clientsList) {
                        clientThread.outputStream.println(clientName+" is connected");
                        clientThread.outputStream.flush();
                    }
                } else if (clientMessage.equals("message_broadcast")) {
                    String message = inputStream.readLine();
                    for (ClientThread clientThread : Server.clientsList) {
                        clientThread.outputStream.println(clientName + ": " + message);
                        clientThread.outputStream.flush();
                    }

                } else if (clientMessage.equals("message_single")) {
                    String messageTo = inputStream.readLine();
                    String message = inputStream.readLine();
                    for (ClientThread clientThread : Server.clientsList) {
                        if (clientThread.clientName.equals(messageTo)) {
                            clientThread.outputStream.println(clientName + ": " + message);
                            clientThread.outputStream.flush();
                        }
                    }
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}

1 个答案:

答案 0 :(得分:1)

只检查一下for循环中的当前实例是否与您所在的实例不同?

            } else if (clientMessage.equals("message_broadcast")) {
                String message = inputStream.readLine();
                for (ClientThread clientThread : Server.clientsList) {
                    if (clientThread != this) {
                        clientThread.outputStream.println(clientName + ": " + message);
                        clientThread.outputStream.flush();
                    }
                }