猿问

Java如何让服务器向每个连接的客户端发送消息

我正在尝试创建一个聊天程序,其中客户端向服务器发送消息,然后服务器将该消息发送到所有连接的客户端以显示它。该程序可以工作,但是当客户端发送消息时,只有它会收到返回的消息,而其余连接的客户端则不会收到任何信息。


客户端代码:



public class Client {


    protected static JTextArea textArea = new JTextArea(20, 30);

    protected static String sendMSG, getMSG;


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

        String hostName = args[0];

        String Username = args[1];

        boolean sending = true;


        try (

            Socket socket = new Socket(hostName, 1010);

            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

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

        ) {

            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));


            //frame setup

            JFrame frame = new JFrame("chat client");

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            //text area

            JScrollPane scrollPane = new JScrollPane(textArea);


            //text field

            JTextField MSGText = new JTextField(5);


            //"send" button

            JButton sMSGB = new JButton("send");

            sMSGB.setPreferredSize(new Dimension(60, 30));

            sMSGB.addActionListener(new ActionListener() {


                public void actionPerformed(ActionEvent event) {

                    sendMSG = MSGText.getText();

                    MSGText.setText("");

                    out.println("<" + Username + ">: " + sendMSG);

                }


            });


            //panel

            JPanel p = new JPanel();

            p.setLayout((new BoxLayout(p, BoxLayout.PAGE_AXIS)));

            p.add(Box.createVerticalStrut(5));

            p.add(scrollPane);

            p.add(Box.createVerticalStrut(5));

            p.add(MSGText);

            p.add(Box.createVerticalStrut(5));

            p.add(sMSGB);

            p.add(Box.createVerticalStrut(5));

            frame.getContentPane().add(p);

                }

            }

        } 

    }       

}   



开满天机
浏览 111回答 1
1回答

白猪掌柜的

非常简单的决定创建一个像列表一样的 PrintWriter 持有者。不要忘记为此系列创建关闭机制!并考虑多线程。public class ServerThread extends Thread {&nbsp; &nbsp; private final Socket socket;&nbsp; &nbsp; private final List<PrintWriter> outs;&nbsp; &nbsp; public ServerThread(Socket socket, List<PrintWriter> outs) {&nbsp; &nbsp; &nbsp; &nbsp; super("ServerThread");&nbsp; &nbsp; &nbsp; &nbsp; this.socket&nbsp; = socket;&nbsp; &nbsp; &nbsp; &nbsp; this.outs = outs;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Opened outs: " + outs.size());&nbsp; &nbsp; }&nbsp; &nbsp; private void sendToAll(String msg) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; for(PrintWriter out: outs) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println(msg);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; try (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrintWriter out = new PrintWriter(socket.getOutputStream(), true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));&nbsp; &nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("stream opened");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outs.add(out);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String getMSGs;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while((getMSGs = in.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("msg received and sent " + getMSGs);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendToAll(getMSGs);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}如果它是一个大项目,最好为消息创建队列
随时随地看视频慕课网APP

相关分类

Java
我要回答