如何向多线程服务器上的所有客户端发送消息?

时间:2019-02-14 14:18:07

标签: java multithreading client-server java-server java-client

我编写了一个简单的多线程服务器,两个客户端可以在该服务器上向服务器发送消息,而服务器一次向所有客户端发送消息。但是我无法使其按预期工作。

我已经通过在Server类中放置所有PrintWriter的列表,然后通过每个PrintWriter打印消息来进行尝试,但这也不起作用。

公共类客户 {

private static final String IP = "10.59.0.188";

private Socket clientSocket;
private PrintWriter toServer;
private BufferedReader fromServer;
private BufferedReader input;
private String serverMessage;
private String clientMessage;
private String name;

public static void main(String[] args) {
    try {
        new Client();
    } catch (Exception e) {
        System.err.println(e);
    }
}

public Client() throws IOException {
    input = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Name: ");
    name = input.readLine();
    openConnection();
    toServer.println(name);

    while (true) {
        clientMessage = input.readLine();
        toServer.println(clientMessage);
    }

    //closeConnection();
}

private void openConnection() throws IOException{
    clientSocket = new Socket(IP, 6666);
    toServer = new PrintWriter(clientSocket.getOutputStream(), true);
    fromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}

}

公共类服务器{

private ServerSocket serverSocket;
private BufferedReader input;
private Vector<ClientProcess> processList;
private int clientCount = 0;
private String serverMessage;

public static void main(String[] args) {
    try {
        new Server();
    } catch (Exception e) {
        System.err.println(e);
    }
}

public Server() throws IOException {
    startServer();

    while (clientCount < 2) {
        waitForNewClient();
    }

    for (ClientProcess clientProcess : processList) {
        clientProcess.start();
        System.out.println("Clientprocess started");
    }        
}

private void startServer() throws IOException {
    processList = new Vector<>();
    input = new BufferedReader(new InputStreamReader(System.in));
    serverSocket = new ServerSocket(6666);
    System.out.println("Server online");
}

private void waitForNewClient() throws IOException {
    System.out.println("Waitin' for new Client...");
    Socket clientSocket = serverSocket.accept();
    ClientProcess clientProcess = new ClientProcess(clientSocket);
    processList.add(clientProcess);
    clientCount++;
}

}

公共类ClientProcess扩展了线程{

private Socket clientSocket;
private PrintWriter toClient;
private BufferedReader fromClient;
private String clientMessage;
private String serverMessage;
private String name;
private BufferedReader input;

public ClientProcess(Socket clientSocket) {
    this.clientSocket = clientSocket;
    input = new BufferedReader(new InputStreamReader(System.in));
}

@Override
public void run() {
    try {
        openClientConnection();
        name = fromClient.readLine();

        do {
            clientMessage = fromClient.readLine();
            System.out.println(name + ": " + clientMessage);
        } while (true);

        //closeClientConnection();
    } catch (IOException e) {
        System.err.println(e);
    }
}

private void openClientConnection() throws IOException {
    toClient = new PrintWriter(clientSocket.getOutputStream());
    fromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    serverMessage = "ServerMessageTest00";
    toClient.println(serverMessage);
    System.out.println("Client Connection Online");
}

}

1 个答案:

答案 0 :(得分:0)

在ClientProcess中,您创建一个新的PrintWriter

来自javadoc:

  

从现有的OutputStream创建新的PrintWriter,而不会自动进行行刷新。

这意味着您的toClient.println(...);已写入服务器的输出缓冲区,但不会发送给客户端,因为您错过了toClient.flush()