简体   繁体   English

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

[英]How do I send a Message to all Clients on a multithreaded Server?

I have written a simple multithreaded Server on which the two Clients can send Messages to the Server an the Server a Message to all the Clients at once. 我编写了一个简单的多线程服务器,两个客户端可以在该服务器上向服务器发送消息,并且服务器可以一次向所有客户端发送消息。 But I can't get it to work as intended. 但是我无法使其按预期工作。

I already tried it by putting a List of all PrintWriters in the Server class and then print the Message through each PrintWriter but this didn't work either. 我已经通过在Server类中放置所有PrintWriter的列表,然后通过每个PrintWriter打印消息来进行尝试,但这也不起作用。

public class Client { 公共类客户{

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()));
}

} }

public class Server { 公共类服务器{

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++;
}

} }

public class ClientProcess extends Thread { 公共类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");
}

} }

In the ClientProcess you create a new PrintWriter . 在ClientProcess中,您将创建一个新的PrintWriter

From the javadoc: 从javadoc:

Creates a new PrintWriter, without automatic line flushing, from anexisting OutputStream. 从现有的OutputStream创建一个新的PrintWriter,而无需自动刷新行。

This means your toClient.println(...); 这意味着您的toClient.println(...); is wrote to the server's output buffer but will not be send to the client because you miss the toClient.flush() . 被写入服务器的输出缓冲区,但不会发送给客户端,因为您错过了toClient.flush()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的多线程服务器/客户端聊天程序使用 sockets 向所有客户端回显消息? - How do I get my multithreaded server/client chat program to echo messages to all clients using sockets? 服务器向所有连接的客户端发送消息 - Server send message to all connected clients websocket从服务器向所有客户端发送消息 - websocket send message from the server to all clients 从服务器向所有连接的客户端发送消息 - send message from server to all connected clients 从服务器向所有客户端发送消息 - Send a message from Server to All Clients 如何在Java客户端服务器程序中将消息发送到多个客户端 - How do i send the message to multiple clients in java Client-Server Program 节俭:向连接到服务器的所有客户端发送消息 - Thrift: Send a message to all clients connected to the server 如何将GraphicsContext从客户端发送到服务器,然后再发送到所有其他客户端? - How do I send a GraphicsContext from a client to a server and then to all of the other clients? 如何与多线程服务器上的所有线程通信? - How do I communicate with all threads on a Multithreaded server? 如何将数据发送到Java中的所有线程客户端? - How do I Send Data to All Threaded Clients in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM