Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java聊天:从单用户到多用户。(涉及serversocket和mysql)_Java_Multithreading_Swing_Sockets_Chat - Fatal编程技术网

Java聊天:从单用户到多用户。(涉及serversocket和mysql)

Java聊天:从单用户到多用户。(涉及serversocket和mysql),java,multithreading,swing,sockets,chat,Java,Multithreading,Swing,Sockets,Chat,我一直在尝试让我现有的聊天类让多个用户都能看到这些消息,但我似乎不知道怎么做 public class user { JFrame chatWindow = null; JButton sendButton = null; JTextField message = null; JTextArea chatArea = null; JPanel chatArea_Container = null;

我一直在尝试让我现有的聊天类让多个用户都能看到这些消息,但我似乎不知道怎么做

public class user 



{

        JFrame chatWindow = null;
        JButton sendButton = null;
        JTextField message = null;  
        JTextArea chatArea = null;
        JPanel chatArea_Container = null;
        JPanel sendButton_Container = null;
        JScrollPane Scroll = null;
        Socket socket = null;
        BufferedReader reader = null;
        PrintWriter writer = null;

        JButton login;
        JTextField username;
        JTextField password;
        JPanel login_Container = null;

        public user(){
            userGUI();
        }
        public void userGUI(){

            chatWindow= new JFrame ("Client");
            username = new JTextField ("");
            password = new JTextField ("");
            login = new JButton ("login");
            sendButton = new JButton ("Send");
            message = new JTextField (4);

            chatArea = new JTextArea (10,12);
            Scroll = new JScrollPane(chatArea);
            chatArea_Container = new JPanel();
            chatArea_Container.setLayout (new GridLayout (1,1));
            chatArea_Container.add(Scroll);

            login_Container = new JPanel();
            login_Container.setLayout(new GridLayout(1,3));
            login_Container.add(username);
            login_Container.add(password);
            login_Container.add(login);

            sendButton_Container = new JPanel();
            sendButton_Container.setLayout(new GridLayout(1,2));
            sendButton_Container.add(message);
            sendButton_Container.add(sendButton);

            chatWindow.setLayout(new BorderLayout());
            chatWindow.add(chatArea_Container,BorderLayout.CENTER);
            chatWindow.add(sendButton_Container,BorderLayout.SOUTH);
            chatWindow.add(login_Container,BorderLayout.NORTH);

            chatWindow.setSize(600, 350);   
            chatWindow.setVisible(true);
            chatWindow.setResizable(false);
            chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);






            Thread main = new Thread(new Runnable(){
                public void run(){
                    try{
                        socket = new Socket ("localhost", 9000);
                        read();
                        write();
                        }
                    catch(Exception ex){
                        ex.printStackTrace();
                    }
                }
            });
                    main.start();
        }

        public void read(){
            Thread read_Thread = new Thread (new Runnable(){
                public void run(){
                    try{
                        reader = new BufferedReader (new InputStreamReader (socket.getInputStream()));
                        while (true){
                            String messageReceived = reader.readLine();
                            chatArea.append("Server says: "+messageReceived+"\n");
                        }
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }

                }
            });
            read_Thread.start();
        }

        public void write(){
            Thread writeThread = new Thread (new Runnable(){
                public void run(){
                    try{ 
                        writer = new PrintWriter(socket.getOutputStream(),true);
                        sendButton.addActionListener(new ActionListener (){
                            public void actionPerformed (ActionEvent e){
                                String sending_message = message.getText();
                                writer.println(sending_message);
                                message.setText("");
                            }
                        });
                    }catch (Exception ex){
                        ex.printStackTrace();
                    } 

                }
            });
            writeThread.start();
        }

    public static void main(String[] args) {
            new user();
    }
    public void setVisible(boolean b) {
        // TODO Auto-generated method stub

    }

}
我的服务器类与这个类几乎相同,但是从一个套接字到另一个serversocket发生了变化

{

        JFrame chatWindow = null;
        JButton sendButton = null;
        JTextField message = null;
        JTextArea chatArea = null;
        JPanel chatArea_Container = null;
        JPanel sendButton_Container = null;
        JScrollPane Scroll = null;
        ServerSocket server = null;
        Socket socket = null;
        BufferedReader reader = null;
        PrintWriter writer = null;
        JButton login;
        JTextField username;
        JTextField password;
        JPanel login_Container = null;


        public server(){
            serverGUI();
        }
        public void serverGUI(){

            chatWindow= new JFrame ("Server");
            username = new JTextField ("");
            password = new JTextField ("");
            login = new JButton ("login");
            login.addActionListener(this);

            sendButton = new JButton ("Send");
            message = new JTextField (4);

            chatArea = new JTextArea (10,12);
            Scroll = new JScrollPane(chatArea);
            chatArea_Container = new JPanel();
            chatArea_Container.setLayout (new GridLayout (1,1));
            chatArea_Container.add(Scroll);

            login_Container = new JPanel();
            login_Container.setLayout(new GridLayout(1,3));
            login_Container.add(username);
            login_Container.add(password);
            login_Container.add(login);

            sendButton_Container = new JPanel();
            sendButton_Container.setLayout(new GridLayout(1,2));
            sendButton_Container.add(message);
            sendButton_Container.add(sendButton);


            chatWindow.setLayout(new BorderLayout());
            chatWindow.add(chatArea_Container,BorderLayout.CENTER);
            chatWindow.add(sendButton_Container,BorderLayout.SOUTH);
            chatWindow.add(login_Container,BorderLayout.NORTH);

            chatWindow.setSize(600, 350);   
            chatWindow.setVisible(true);
            chatWindow.setResizable(false);
            chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Thread main = new Thread (new Runnable(){
                public void run(){
                    try{
                    server = new ServerSocket(9000);
                        while(true){
                            socket = server.accept();
                            read();
                            write();
                        }
                    }catch(Exception ex){
                        ex.printStackTrace();

                    }
                }
            });
            main.start();
        }



        public void read(){
            Thread read_Thread = new Thread (new Runnable(){
                public void run(){
                    try{
                        reader = new BufferedReader (new InputStreamReader (socket.getInputStream()));
                        while (true){
                            String messageReceived = reader.readLine();
                            chatArea.append("User says: "+messageReceived+"\n");
                        }
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }

                }
            });
            read_Thread.start();
        }

        public void write(){
            Thread writeThread = new Thread (new Runnable(){
                public void run(){
                    try{ 
                        writer = new PrintWriter(socket.getOutputStream(),true);
                        sendButton.addActionListener(new ActionListener (){
                            public void actionPerformed (ActionEvent e){
                                String sending_message = message.getText();
                                writer.println(sending_message);
                                message.setText("");
                            }
                        });
                    }catch (Exception ex){
                        ex.printStackTrace();
                    } 

                }
            });
            writeThread.start();
        }
    public static void main(String[] args) {

            new server();

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

}
我需要添加一种方法,使消息被多个用户看到,但我不知道如何做到这一点

你会怎么解决这个问题?我在看这个:


我基本上需要修改服务器类,使其没有读写方法,只打开两个用户窗口吗?

首先,您违反了Swing的单线程规则,Swing不是线程安全的,您应该只在事件调度线程的上下文中更新UI。有关更多详细信息和可能的解决方案,请参阅。对于您的问题,您的服务器需要跟踪所有活动的客户端连接。每次向服务器发送消息时,服务器都需要将此消息重新广播给所有其他客户端谢谢你的程序员。我还是一个初学者,我需要更多关于重播的信息。你能用一个小的伪代码片段详细描述一下吗?