如何通过Internet对服务器进行Java RMI调用

时间:2012-10-15 13:19:26

标签: java rmi

我尝试了一个简单的java rmi程序,客户端对服务器进行远程调用,服务器返回一个String。它与通过lan连接的两台计算机完美配合。如何通过局域网连接到服务器的客户端实现远程调用,但服务器和客户端都连接到互联网?我通过将Client.java中的“localhost”更改为服务器的ip地址来尝试以下简单代码。没工作。我需要做什么远程通话?

//远程接口 - > MyInterface.java

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MyInterface extends Remote
{
    public String SaySomething() throws RemoteException;
}

// Client.java

import java.rmi.*;
import java.rmi.registry.*;

public class Client
{
    public static void main(String []args)
    {
        System.setProperty( "java.security.policy", "client.policy" );
        if (System.getSecurityManager() == null)
                    System.setSecurityManager(new RMISecurityManager());
        try {
                        String name = "MyInterface";
                        Registry registry = LocateRegistry.getRegistry("localhost",4501);
                        MyInterface mi = (MyInterface) registry.lookup(name);
            System.out.println(mi.SaySomething());
                } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

// Server.java

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;

public class Server implements MyInterface
{
    public Server()
    {
        super();
    }
    public String SaySomething()
    {
        return "Server Speaking";
    }
    public static void main(String []args)
    {
        System.setProperty( "java.security.policy", "server.policy" );
        if(System.getSecurityManager() == null)
           System.setSecurityManager(new RMISecurityManager());
            try {
                        String name = "MyInterface";
                        MyInterface mi = new Server();
                        MyInterface stub =(MyInterface) UnicastRemoteObject.exportObject(mi,4501);
                        Registry registry = LocateRegistry.createRegistry(4501);
                        registry.rebind(name, stub);
                        System.out.println("Server bound");
            } catch (Exception e) {
            System.out.println("oops");
                }
    }
}

我收到以下异常: java.security.AccessControlException:拒绝访问(“java.net.SocketPermission”“x.x.x.x:4501”“connect,resolve”)        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)        在java.security.AccessController.checkPermission(AccessController.java:555)        在java.lang.SecurityManager.checkPermission(SecurityManager.java:549)        在java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)        在java.net.Socket.connect(Socket.java:574)        在java.net.Socket.connect(Socket.java:528)        在java.net.Socket。(Socket.java:425)        在java.net.Socket。(Socket.java:208)        at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)        at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)        at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)        at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)        at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340)        at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)        在Client.main(Client.java:14)

2 个答案:

答案 0 :(得分:0)

除非您使用代码库功能,否则您不需要RMI的安全管理器。删除它。

答案 1 :(得分:-1)

您应该检查一下您的访问权限。

如果您的服务器是家用PC,则应检查以确保路由器允许传入连接(默认情况下会阻止)。

如果您的服务器是办公室PC,则它可能会被防火墙阻止。