title: Rmi in Java - Chat Application with need of modification, but How?
body: <p>I have made java RMI application - Chat.</p>
  
  <p>Chat Class:</p>
  
  <pre><code>public class Chat extends UnicastRemoteObject implements ChatInterface  {
  
      public String name;
      public ChatInterface client=null;
  
      public Chat(String n)  throws RemoteException { 
          this.name=n;   
      }
      public String getName() throws RemoteException {
          return this.name;
      }
  
      public void setClient(ChatInterface c){
          client=c;
      }
  
      public ChatInterface getClient(){
          return client;
      }
  
      public void send(String s) throws RemoteException{
          System.out.println(s);
      }   
  }
  </code></pre>
  
  <p>ChatClient class:</p>
  
  <pre><code>    import java.rmi.*;
      import java.rmi.server.*;
      import java.util.*;
  
      public class ChatClient {
          public static void main (String[] argv) {
              try {
                  System.setProperty("java.rmi.server.hostname","127.0.0.1");
                  System.setSecurityManager(new RMISecurityManager());
                  Scanner s=new Scanner(System.in);
                  System.out.println("Enter Your name and press Enter:");
                  String name=s.nextLine().trim();
                  ChatInterface client = new Chat(name);
                  ChatInterface server = (ChatInterface)Naming.lookup("rmi://localhost/ABC");
                  String msg="["+client.getName()+"] got connected";
                  server.send(msg);
                  System.out.println("[System] Chat Remote Object is ready:");
                  server.setClient(client);
  
                  while(true){
                      msg=s.nextLine().trim();
                      msg="["+client.getName()+"] "+msg;                  
                      server.send(msg);
                  }
  
              }catch (Exception e) {
                  System.out.println("[System] Server failed: " + e);
              }
          }
      }
  </code></pre>
  
  <p>ChatInterface interface:</p>
  
  <pre><code>public interface ChatInterface extends Remote{
      public String getName() throws RemoteException;
      public void send(String msg) throws RemoteException;
      public void setClient(ChatInterface c)throws RemoteException;
      public ChatInterface getClient() throws RemoteException;
  }
  </code></pre>
  
  <p>ChatServer class:</p>
  
  <pre><code>public class ChatServer {
      public static void main (String[] argv) {
          try {
              System.setProperty("java.rmi.server.hostname","127.0.0.1");
              System.setSecurityManager(new RMISecurityManager());
              Scanner s=new Scanner(System.in);
              System.out.println("Enter Your name and press Enter:");
              String name=s.nextLine().trim();
  
              Chat server = new Chat(name);   
  
              Naming.rebind("rmi://localhost/ABC", server);
  
              System.out.println("[System] Chat Remote Object is ready:");
  
              while(true){
                  String msg=s.nextLine().trim();
                  if (server.getClient()!=null){
                      ChatInterface client=server.getClient();
                      msg="["+server.getName()+"] "+msg;
                      client.send(msg);
                  }   
              }
          }catch (Exception e) {
              System.out.println("[System] Server failed: " + e);
          }
      }
  }
  </code></pre>
  
  <p>Chat - the same code in both projects</p>
  
  <p>ChatInterface - the same code in both projects too</p>
  
  <p>ChatClient - ChatServer - they ones that have different code</p>
  
  <hr>
  
  <p>It looks like this: <a href="https://i.stack.imgur.com/kfQpg.jpg" rel="nofollow noreferrer">How it looks inside Eclipse (hierarchy)</a></p>
  
  <p>And problem is, that this chat works something like...
  We got all messages (like whole history) but when user send new message.
  For example, we have user1, user2, user3.</p>
  
  <ul>
  <li>User1 send message: 'FUN!!!'</li>
  <li>User2 and User3 see nothing.</li>
  <li>User2 send "okkk"</li>
  <li>Now user1 sees still only "FUN!!!"</li>
  <li>User2 see "FUN!!!" and "okkk"</li>
  </ul>
  
  <p>And User3 sees nothing, cuz didn't send any message, so have nothing.</p>
  
  <p>And that's bad :/. It's like user have to send something to get messages. Even only "space", but still something. Otherwise he won't get any message.</p>
  
  <p>I need to make this to automatically send all users new messages when they are send by one user. Like "update".'</p>
  
  <p>It needs to be like</p>
  
  <ul>
  <li>User1 got nothing</li>
  <li>User2 got nothing</li>
  <li>User3 got nothing</li>
  <li>Users3 sends 'AAAA'</li>
  <li>All users have now 'AAAA'</li>
  </ul>