'how to make multi-clients server chat app with java rmi

i made chat room using java rmi but the problem is that the message appears only to one client(the last to connect) for example 3 clients connect to the server(client1,client2,client3) if client3 sends a message it appears only in server's console . and if server sends a message it appears only to client3's console. i want to make the message to be shown to all the users connected to the server.

here is my code( i used eclipse for this project):

serveur.java

import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
 import  java.rmi.registry.*;
public class serveur  {
public static void main (String[] argv) {
    try {

            Scanner s = new Scanner(System.in);
            System.out.println("Entrez votre nom et appuyez sur Entree:");
            String name=s.nextLine().trim();
 
            chat server = new chat(name);
        Registry registre = LocateRegistry.createRegistry(1099);

        Naming.rebind( "CHAT",server);

            System.out.println(" Le CHAT est pret:");

            while(true){
                String msg = s.nextLine().trim();
                if (server.getClient() != null){
                    chatint Client = server.getClient();
                    msg = server.getName()+" : "+msg;
                    Client.send(msg);





                }
            }
 
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

client.java

public class client   {


    public static void main(String args[]) {

        try {

            Scanner s = new Scanner(in);
            out.println("Entrez votre nom et appuyez sur Entrez:");
            String name = s.nextLine().trim();
            chatint client = new chat(name);

            chatint server = (chatint)Naming.lookup("CHAT");
            String msg = "["+client.getName()+"] s'est connecte";
            server.send(msg);

            out.println("[System] Le CHAT est pret:");

            server.setClient(client);


            while(true){
                msg = s.nextLine().trim();
                msg = client.getName()+" : "+msg;
                server.send(msg);



            }

        }catch (Exception e) {
            e.printStackTrace();
        }


    }


}

chatint.java

import java.rmi.*;

public interface chatint extends Remote{
    public String getName() throws RemoteException;
    
    public void send(String s) throws RemoteException;
    
    public void setClient( chatint c) throws RemoteException;
    
    public  chatint getClient() throws RemoteException;
}

chat.java

package projetSid;


import java.rmi.*;
import java.rmi.server.*;


public class chat extends UnicastRemoteObject implements chatint {
 
    public String name;
    public chatint client = null;
 
    public chat(String n) throws RemoteException { 
        this.name = n;   
    }
    public String getName() throws RemoteException {
        return this.name;
    }
 
    public void setClient(chatint c){
        client = c;
    }
 
    public chatint getClient(){
        return client;
    }
 

    public void send(String s) throws RemoteException{
        System.out.println(s);
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source