简体   繁体   中英

Multi Chat Alert Program only broadcasting to 1 client

I've started this multi chat thread alert system and I've successfully gotten multiple clients on the server, but when broadcasting the message to everyone, it only interacts with the initial client sending the message and the sever only, the other client does not receive the message. Here are the codes I'm working with

Client 1    

package popup;

    import java.io.*;
    import java.net.*;
    import javax.swing.*;

    public class ClientJFrame extends javax.swing.JFrame {

        static Socket s;
        static DataInputStream din;
        static DataOutputStream dout;

        public ClientJFrame() {
            super("Client 1");
            initComponents();
        }



        private void alertButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
            try {
                String msgout = "Alert client 1\n";
                dout.writeUTF(msgout);
            } catch (Exception e) {
            }
        }                                           


        public static void main(String args[]) {


            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new ClientJFrame().setVisible(true);


                }
            });
            try {
                s = new Socket("127.0.0.1", 111);
                din = new DataInputStream(s.getInputStream());
                dout = new DataOutputStream(s.getOutputStream());
                String msgin = "";
                while (true) {

                    msgin = din.readUTF();
                    messageArea.append(msgin);
                    JOptionPane.showMessageDialog(null, "BITCH WE ON FIRE");
                    s.close();
                    System.exit(0);

                }
            } catch (Exception e) {
            }
        }

        // Variables declaration - do not modify                     
        private javax.swing.JButton alertButton;
        private javax.swing.JScrollPane jScrollPane1;
        private static javax.swing.JTextArea messageArea;
        // End of variables declaration                   
    }

Server

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class TestJFrame extends javax.swing.JFrame {

    static ServerSocket listener;
    static Socket s;
    private static final int PORT = 111;

    public TestJFrame() {
        super("Main");
        initComponents();
    }

public static class Handler extends Thread {

        private final Socket socket;
        private DataInputStream in;
        private DataOutputStream out;

        public Handler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {

            try {
                in = new DataInputStream(socket.getInputStream());
                messageArea.append("in\n");
                out = new DataOutputStream(socket.getOutputStream());
                messageArea.append("Out\n");
            } catch (IOException e) {
            }
            while (true) {
                try {
                    String input = in.readUTF();
                    messageArea.append(input);
                    out.writeUTF("We on Fire!!!\n");

                } catch (IOException ex) {
                    Logger.getLogger(TestJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }
    }

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


        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestJFrame().setVisible(false);
                createAndShowGUI();
            }

        });

        listener = new ServerSocket(PORT);
        try {
            while (true) {
                new Handler(listener.accept()).start();
            }
        } finally {
            listener.close();
        }

    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton alertButton;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTextArea messageArea;
    // End of variables declaration                   
}

When a client connects to the server, add him to a list, so you always know who's connected. The same goes for when he disconnects.

When a client sends a message, process it however you want, then iterate over the list of connected clients and send them the message.

Take a look at the observer pattern , I think it will help for your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM