1

java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")

My server side is working perfectly, no errors on the server.. while when I run my client code, I get this access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve") error.

Please, any expert help me :(

This is my client code

/**
 *
 * @author saqibhussain
 */
public class ChatClient extends UnicastRemoteObject implements ChatClientIF, Runnable {
public ChatClient() throws RemoteException {
}
private ChatServerIF chat;
private String name = null;

protected ChatClient(String name, ChatServerIF chat) throws RemoteException {        this.name = name;
    this.chat = chat;
    chat.RegisterChatClient(this);
}

public void retrieveMessage(String msg) throws RemoteException {
    System.out.println(msg);
}

public void run() {
    Scanner scaner = new Scanner(System.in);
    String message;
    while (true) {
        try {
            message = scaner.nextLine();
            chat.broadcastMessage(name + " : " + message);
        } catch (RemoteException ex) {
            Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

public static void main(String[] args) throws NotBoundException, MalformedURLException, RemoteException {
        System.setSecurityManager(new RMISecurityManager());

        try {
        String url = "rmi://localhost/RMIChatServer";
        ChatServerIF remoteObject = (ChatServerIF) Naming.lookup(url);
        System.out.println("Got remote object");
        new Thread(new ChatClient(args[0], remoteObject)).start();

        } catch (Exception e) {
        System.out.println(e);
        }
}
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3125051
  • 9
  • 1
  • 1
  • 2

2 Answers2

1

Add a security policy to your client application. You can download a sample policy from here: http://www.comp.lancs.ac.uk/~weerasin/csc253/tutorials/week8code/client.policy

After that start your client with the following vm argument

java -Djava.security.policy==client.policy

Be carefull in production environments since the given policy grants permission to any operation performed by your client.

Diversity
  • 1,890
  • 13
  • 20
0

You've defined a SecurityManager but you haven't granted yourself enough permissions to execute your code. You need to write yourself a policy file and specify it to the JVM when starting via -Djava.security.policy=....

Or, just remove the security manager. You don't need it unless you're using the RMI Codebase feature.

user207421
  • 305,947
  • 44
  • 307
  • 483