Java RMI客户端 - 服务器聊天

时间:2017-05-01 19:55:11

标签: java rmi

我将使用两个用户创建一个简单的聊天程序。一个用户将从服务器端聊天,而另一个用户将从RMI应用程序的客户端聊天。在本教程中使用Eclipse创建的项目的文件结构如下所示:

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;

}

与我使用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);
        }
    }
}

我用

创建了security.policy
grant {
        permission java.security.AllPermission;
  };

我的项目的VM参数:

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

我收到了这个错误:

    [System] Server failed: java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:8888" "listen,resolve")

0 个答案:

没有答案