Sending a message to all clients (Client - Server communication)

前端 未结 3 1994
北海茫月
北海茫月 2020-11-29 04:29

So now, I am making a client server app based multithread. In server side, I make a thread for everysingle connection that accepted.

In thread class, I make a method

相关标签:
3条回答
  • 2020-11-29 04:39

    Check out zeroMQ. There are methods known as "pub sub" or "publish subscribe" that will do what you want. You can also use it to communicate between your threads. It is an amazing library in my opinion. It has java or jzmq bindings along with over 30+ others as well so you should be able to use it in your program.

    http://www.zeromq.org/

    0 讨论(0)
  • 2020-11-29 04:51

    There is no method in server socket to send data or message to all running clinet threads. Please go through the ServerThread.java program which is calling the sendToAll usng server.

    // ... and have the server send it to all clients
    server.sendToAll( message );
    
    0 讨论(0)
  • 2020-11-29 04:59

    The below answer, is not recommended for a full fledged server, as for this you should use Java EE with servlets, web services etc.

    This is only intended where a few computers want to connect to perform a specific task, and using simple Java sockets is not a general problem. Think of distributed computing or multi-player gaming.

    EDIT: I've - since first post - greatly updated this architecture, now tested and thread-safe. Anybody who needs it may download it here.

    Simply use (directly, or by subclassing) Server and Client, start() them, and everything is ready. Read the inline comments for more powerful options.


    While communication between clients are fairly complicated, I'll try to simplify it, the most possible.

    Here are the points, in the server:

    • Keeping a list of connected clients.
    • Defining a thread, for server input.
    • Defining a queue of the received messages.
    • A thread polling from the queue, and work with it.
    • Some utility methods for sending messages.

    And for the client:

    • Defining a thread, for client input.
    • Defining a queue of the received messages.
    • A thread polling from the queue, and work with it.

    Here's the Server class:

    public class Server {
        private ArrayList<ConnectionToClient> clientList;
        private LinkedBlockingQueue<Object> messages;
        private ServerSocket serverSocket;
    
        public Server(int port) {
            clientList = new ArrayList<ConnectionToClient>();
            messages = new LinkedBlockingQueue<Object>();
            serverSocket = new ServerSocket(port);
    
            Thread accept = new Thread() {
                public void run(){
                    while(true){
                        try{
                            Socket s = serverSocket.accept();
                            clientList.add(new ConnectionToClient(s));
                        }
                        catch(IOException e){ e.printStackTrace(); }
                    }
                }
            };
    
            accept.setDaemon(true);
            accept.start();
    
            Thread messageHandling = new Thread() {
                public void run(){
                    while(true){
                        try{
                            Object message = messages.take();
                            // Do some handling here...
                            System.out.println("Message Received: " + message);
                        }
                        catch(InterruptedException e){ }
                    }
                }
            };
    
            messageHandling.setDaemon(true);
            messageHandling.start();
        }
        
        private class ConnectionToClient {
            ObjectInputStream in;
            ObjectOutputStream out;
            Socket socket;
    
            ConnectionToClient(Socket socket) throws IOException {
                this.socket = socket;
                in = new ObjectInputStream(socket.getInputStream());
                out = new ObjectOutputStream(socket.getOutputStream());
    
                Thread read = new Thread(){
                    public void run(){
                        while(true){
                            try{
                                Object obj = in.readObject();
                                messages.put(obj);
                            }
                            catch(IOException e){ e.printStackTrace(); }
                        }
                    }
                };
    
                read.setDaemon(true); // terminate when main ends
                read.start();
            }
    
            public void write(Object obj) {
                try{
                    out.writeObject(obj);
                }
                catch(IOException e){ e.printStackTrace(); }
            }
        }
    
        public void sendToOne(int index, Object message)throws IndexOutOfBoundsException {
            clientList.get(index).write(message);
        }
    
        public void sendToAll(Object message){
            for(ConnectionToClient client : clientList)
                client.write(message);
        }
    
    }
    

    And here for the Client class:

    public class Client {
        private ConnectionToServer server;
        private LinkedBlockingQueue<Object> messages;
        private Socket socket;
    
        public Client(String IPAddress, int port) throws IOException{
            socket = new Socket(IPAddress, port);
            messages = new LinkedBlokingQueue<Object>();
            server = new ConnecionToServer(socket);
    
            Thread messageHandling = new Thread() {
                public void run(){
                    while(true){
                        try{
                            Object message = messages.take();
                            // Do some handling here...
                            System.out.println("Message Received: " + message);
                        }
                        catch(InterruptedException e){ }
                    }
                }
            };
    
            messageHandling.setDaemon(true);
            messageHandling.start();
        }
    
        private class ConnectionToServer {
            ObjectInputStream in;
            ObjectOutputStream out;
            Socket socket;
    
            ConnectionToServer(Socket socket) throws IOException {
                this.socket = socket;
                in = new ObjectInputStream(socket.getInputStream());
                out = new ObjectOutputStream(socket.getOutputStream());
    
                Thread read = new Thread(){
                    public void run(){
                        while(true){
                            try{
                                Object obj = in.readObject();
                                messages.put(obj);
                            }
                            catch(IOException e){ e.printStackTrace(); }
                        }
                    }
                };
    
                read.setDaemon(true);
                read.start();
            }
    
            private void write(Object obj) {
                try{
                    out.writeObject(obj);
                }
                catch(IOException e){ e.printStackTrace(); }
            }
    
    
        }
    
        public void send(Object obj) {
            server.write(obj);
        }
    }
    
    0 讨论(0)
提交回复
热议问题