无法在简单的聊天应用程序中读取BlockingQueue

时间:2015-11-03 00:46:41

标签: java multithreading concurrency

我遇到了BlockingQueue的问题(至少我是这么认为的)。我只是在广播模式下在套接字上写一个简单的聊天应用程序。我有2个公共类:客户端和服务器。他们每个人都有一个私有类:ClientHandler / ServerHandler。服务器有2个线程:一个应该接受并添加到ArrayList客户端实例,另一个用于处理masseges。客户端有一个用于显示消息的线程和一个用于向服务器发送消息的事件。每个客户端都拥有自己的LinkedBlockingQueue,Server也有一个。我的代码中没有错误,但无法看到任何消息:/

代码:

服务器类:

man git diff

客户端类:

public class Server {
    private final int serverPort = 23456;
    private ServerSocket serverSocket = null;
    private ArrayList<ClientHandler> clientList = null;
     private LinkedBlockingQueue<String> messages;

    JFrame frame = null;
    JTextArea messageArea = null;

    public Server(){
        clientList = new ArrayList<ClientHandler>();
        messages = new LinkedBlockingQueue<String>();
        frame = new JFrame("Serwer");
        messageArea = new JTextArea(20, 60);
        messageArea.setEditable(false);
        frame.getContentPane().add(new JScrollPane(messageArea), "Center");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

        messageArea.append("Uruchamianie serwera na porcie " + Integer.toString(serverPort) + "...\n");        
        try {
            serverSocket = new ServerSocket(serverPort);
            messageArea.append("Serwer aplikacji chat został uruchomiony.\n");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void start(){
        Thread accept = new Thread() {
            public void run(){
                while(true){
                    try{
                        Socket s = serverSocket.accept();
                        clientList.add(new ClientHandler(s));
                    } catch(IOException e){ 
                        e.printStackTrace(); 
                    }
                }
            }
        };

        accept.setDaemon(true);
        accept.start();

        Thread messageHandling = new Thread() {
            public void run(){
                while(true){
                    try{
                        System.out.println("wysylam");
                        String message = messages.take();
                        messageArea.append(message + "\n");
                        send(message);
                    }
                    catch(InterruptedException e){ }
                }
            }
        };

        messageHandling.setDaemon(true);
        messageHandling.start();
    }

    public void close(){
        try {
            for(ClientHandler client : clientList){
                client.in.close();
                client.out.close();
                client.clientSocket.close();
            }
            serverSocket.close();
            messageArea.append("Serwer aplikacji chat został zatrzymany.\n");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void send(String message){
        for(ClientHandler client : clientList){
            client.out.write(message);
        }
    }

    private class ClientHandler{
        Socket clientSocket = null;
        BufferedReader in = null;
        PrintWriter out = null;

        ClientHandler(Socket socket){
            clientSocket = socket;
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                Thread read = new Thread(){
                    public void run(){
                        while(true){
                            try{
                                String msg = in.readLine();
                                messages.put(msg);
                                System.out.println(msg);
                            }
                            catch(IOException | InterruptedException e){ 
                                e.printStackTrace(); 
                            }
                        }
                    }
                };

                read.setDaemon(true);
                read.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

一个简单的测试类:

public Client(int id) {
        messages = new LinkedBlockingQueue<String>();
        this.id = id;
        frame = new JFrame("Klient");
        dataField = new JTextField(40);
        messageArea = new JTextArea(8, 60);

        messageArea.setEditable(false);
        frame.getContentPane().add(dataField, "North");
        frame.getContentPane().add(new JScrollPane(messageArea), "Center");

        name = JOptionPane.showInputDialog(frame, "Podaj nazwę użytkownika:");
        frame.setTitle(name);
        frame.pack();
        frame.setVisible(true);

        try {
            socket = new Socket("127.0.0.1", serverPort);
            server = new ServerHandler(socket);
            dataField.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String message = dataField.getText();
                    dataField.setText("");
                    send(message);
                }
            });
            run();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    public void run(){
        Thread messageHandling = new Thread() {
            public void run(){
                while(true){
                    try{
                        String message = messages.take();
                        messageArea.append(message + "\n");
                    }
                    catch(InterruptedException e){ }
                }
            }
        };

        messageHandling.setDaemon(true);
        messageHandling.start();
    }

    public void close(){
        try {
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private class ServerHandler {
        BufferedReader in = null;
        PrintWriter out = null;
        Socket socket = null;

        ServerHandler(Socket socket) throws IOException {
            this.socket = socket;
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);

            Thread read = new Thread(){
                public void run(){
                    while(true){
                        try{
                            String msg = in.readLine();
                            messages.put(msg);
                        }
                        catch(IOException | InterruptedException e){ e.printStackTrace(); }
                    }
                }
            };

            read.setDaemon(true);
            read.start();
        }
    }

    private void send(String msg) {
        System.out.println(msg);
        server.out.write(msg);
    }

    public String getName(){
        return name;
    }
}

知道我做错了吗?

0 个答案:

没有答案