第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Java如何讓服務(wù)器向每個(gè)連接的客戶端發(fā)送消息

Java如何讓服務(wù)器向每個(gè)連接的客戶端發(fā)送消息

開滿天機(jī) 2023-06-21 15:49:21
我正在嘗試創(chuàng)建一個(gè)聊天程序,其中客戶端向服務(wù)器發(fā)送消息,然后服務(wù)器將該消息發(fā)送到所有連接的客戶端以顯示它。該程序可以工作,但是當(dāng)客戶端發(fā)送消息時(shí),只有它會(huì)收到返回的消息,而其余連接的客戶端則不會(huì)收到任何信息。客戶端代碼: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);                }            }        }     }       }   
查看完整描述

1 回答

?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

非常簡(jiǎn)單的決定創(chuàng)建一個(gè)像列表一樣的 PrintWriter 持有者。不要忘記為此系列創(chuàng)建關(guān)閉機(jī)制!并考慮多線程。


public class ServerThread extends Thread {

    private final Socket socket;

    private final List<PrintWriter> outs;


    public ServerThread(Socket socket, List<PrintWriter> outs) {

        super("ServerThread");

        this.socket  = socket;

        this.outs = outs;

        System.out.println("Opened outs: " + outs.size());

    }


    private void sendToAll(String msg) throws IOException {

        for(PrintWriter out: outs) {

            out.println(msg);

        }

    }


    public void run() {

        try (

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

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

        ) {

            System.out.println("stream opened");

            outs.add(out);

            String getMSGs;

            while((getMSGs = in.readLine()) != null) {

                System.out.println("msg received and sent " + getMSGs);

                sendToAll(getMSGs);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

如果它是一個(gè)大項(xiàng)目,最好為消息創(chuàng)建隊(duì)列


查看完整回答
反對(duì) 回復(fù) 2023-06-21
  • 1 回答
  • 0 關(guān)注
  • 135 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)