如何使服务器能够接受来自具有不同套接字连接的客户端的多个输入流?

How can I enable a server to accept multiple input streams from clients with distinct socket connections?

我正在尝试创建一个简单的聊天服务器,允许多个不同的客户端通过服务器输出控制台相互聊天。每个客户端都有自己的线程写入服务器,并可以通过控制台在服务器的标准输出上查看结果。但是,我似乎无法让 BufferedReader 接收来自多个客户端套接字连接的消息。

目前,第一个客户端线程通过其套接字获得对 BufferedReader 的独占访问权。但是,我希望多个客户端连接到服务器的输入流 reader 并让它等待来自具有不同套接字连接的多个客户端线程的输入。我希望客户端能够同时 post 到服务器。使用或不使用 BufferedReader 作为我的输入流 reader,我将如何完成此操作?

public class chatServer {

    public chatServer() throws IOException {

        int PORT = 8189;

        try (ServerSocket server = new ServerSocket(PORT)) {
            System.out.println("The server is running at "
                    + InetAddress.getByName(null) + "...");
            String rules = "The rules of this server are as follows:\n"
                    + "1.Respect your fellow chatters\n"
                    + "2.Vulgar language will result in punishment\n"
                    + "3.We reserve the right to ban you at any time.\n"
                    + "Enjoy!";

            System.out.println(rules + "\n");

            while (true) {
                try {
                    new clientHandler(server.accept()).run();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        chatServer cs = new chatServer();
    }

    class clientHandler implements Runnable {

        Socket socket;

        public clientHandler(Socket socket) {
            this.socket = socket;
        }

        public void run() {

            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));

                String line;
                while (true) {
                    line = in.readLine();
                    if ((line == null) || line.equalsIgnoreCase("exit")) {
                        // socket.close();
                    } else {
                        System.out.println(socket.getPort() + " > " + line);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }
}

public class chatClient {

    private Socket socket;
    private String name = "";   
    private String IP = "127.0.0.1";
    private int PORT = 8189;


    public chatClient(String name, String IP, int PORT) throws UnknownHostException, IOException{

        this.name = name;
        socket = new Socket(this.IP,this.PORT);

    }

    public static void main(String[] args) throws UnknownHostException, IOException{

        chatClient c1 = new chatClient("John",null,0);
        chatClient.connect(c1);

    }

    public static void connect(chatClient cc) throws IOException {

        Socket socket = cc.socket; 
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println("Welcome " + cc.name);

        String message = "";
        boolean done = false;
        Scanner stdin = new Scanner(System.in);
        System.out.println("Type your message here:");

        while(!done){
        System.out.print("> ");
        message = stdin.nextLine();
        out.println(message);

        if(message.trim().equalsIgnoreCase("exit")){
            done = true;
        }

        }
    }

}

更新: 我正在寻找一种 suitable/alternative 方法来实现服务器的功能,该服务器接受来自不同客户端的多个 post套接字连接?如果我当前的实现无法做到这一点,那么我该如何修改才能做到这一点?

A BufferedReader 是围绕单个 reader 构建的,后者又直接或间接连接到单个数据源。一旦构建就无法更改。

总之你的问题没有意义。每个客户端都有一个不同的套接字,而套接字又具有其唯一的输入和输出流,而每个客户端又必须将其包装在不同的 BufferedTeader 中。没有一个在所有客户端之间共享。

编辑 您的实际问题与 BufferedReaders 无关。它在这里:

new clientHandler(server.accept()).run();

您是 运行 内联处理程序而不是单独的线程。 run() 应该是

new Thread(new clientHandler(socket.accept())).start();