title: Java RMI Client-Server Chat
body: <p>I will create a simple chat program using two users. One user will be chatting from the server side, whilst the other user would be chatting from the client side of the RMI application. The structure of the files for the projects created using Eclipse throughout this tutorials is shown below:</p>
  
  <pre><code>SimpleChatClient that contains
  
   Chat.java
  
  import java.rmi.*;
  
  import java.rmi.server.*;
  
  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);
      }   
  }
  ChatClient.java
  
  import java.rmi.*;
  import java.rmi.server.*;
  import java.util.*;
  
  public class ChatClient {
      public static void main (String[] argv) {
          try {
                  System.setProperty("java.security.policy","file:./security.policy");
                  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);
              }
          }
  
  }
  ChatInterface.java
  import java.rmi.*;
  
  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>In the same way i created the Server with Chat.java, ChatInterface</p>
  
  <p>ChatServer.java</p>
  
  <pre><code>import java.rmi.*;
  import java.rmi.registry.LocateRegistry;
  import java.rmi.registry.Registry;
  import java.rmi.server.*;
  import java.util.*;
  
  public class ChatServer {
  public static void main (String[] argv) {
      try {
              System.setProperty("java.security.policy","file:./security.policy");
              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);   
              Registry registry = LocateRegistry.createRegistry(8888);
              registry.bind("Chat", server);
              //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>I created the security.policy with </p>
  
  <pre><code>grant {
          permission java.security.AllPermission;
    };
  </code></pre>
  
  <p>And the VM Arguments for my project :</p>
  
  <pre><code>-Djava.security.policy=file:${workspace_loc}/SimpleChatServer/security.policy
  -Djava.rmi.server.codebase=file:${workspace_loc}/SimpleChatServer/bin/
  </code></pre>
  
  <p>And I got this error :</p>
  
  <pre><code>    [System] Server failed: java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:8888" "listen,resolve")
  </code></pre>