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

时间:2016-04-08 04:49:02

标签: java multithreading sockets inputstream bufferedreader

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

目前,第一个客户端线程通过它的套接字获得对BufferedReader的独占访问权限。但是,我希望多个客户端连接到服务器的输入流读取器,并让它等待来自具有不同套接字连接的多个客户端线程的输入。我希望客户能够同时发布到服务器。如果有BufferedReader作为我的输入流阅读器,我将如何实现这一目标?

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

        }
    }

}

更新:我正在寻找一种合适/替代方法来实现服务器的功能,该服务器接受来自具有不同套接字连接的各种客户端的多个帖子?如果我当前的实现不能这样做那么我该如何修改呢?

0 个答案:

没有答案
相关问题