0

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:

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;

}

In the same way i created the Server with Chat.java, ChatInterface

ChatServer.java

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);
        }
    }
}

I created the security.policy with

grant {
        permission java.security.AllPermission;
  };

And the VM Arguments for my project :

-Djava.security.policy=file:${workspace_loc}/SimpleChatServer/security.policy
-Djava.rmi.server.codebase=file:${workspace_loc}/SimpleChatServer/bin/

And I got this error :

    [System] Server failed: java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:8888" "listen,resolve")
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    You might want to print the `System` properties to ensure the ${workspace_loc} was resolved in the VM arguments. Also, see this question which also mentions a bit about the potential issue: http://stackoverflow.com/questions/179799/java-rmi-tutorial-accesscontrolexception-access-denied-java-io-filepermissio – KevinO May 01 '17 at 20:04
  • As you're not using the codebase feature, just remove the security manager. You don't need it. – user207421 May 01 '17 at 22:35
  • When I run the Client I got this : [System] Server failed: java.rmi.NotBoundException: ABC – Madalina Cristina May 02 '17 at 04:25
  • The VM arguments look like this -Djava.security.policy=file:/C:/Users/Madalina/workspace/SimpleChatClient/bin/security.policy -Djava.rmi.server.codebase=file:/C:/Users/Madalina/workspace/SimpleChatClient/bin/ – Madalina Cristina May 02 '17 at 04:28
  • Hard to see what else you can expect when you bind as `"Chat"` and lookup as `"ABC"`. NB Removing the security manager => removing the .policy file as well. – user207421 May 02 '17 at 09:54

0 Answers0