为什么Java RMI回调在NAT路由器后面不起作用?

时间:2014-11-11 17:14:35

标签: java rmi router nat behind

我正在尝试使用一个简单的程序在Java RMI上进行回调,并且它在localhost和LAN中正常工作,但我在NAT路由器后面做这个问题。

如果我的服务器实现extends UnicastRemoteObject,我会拒绝连接。 如果我不extends UnicastRemoteObject并且我手动导出对象(UnicastRemoteObject.exportObject(services, 0);),则服务器在客户端调用远程方法时执行操作,并且当服务器调用回调方法时,它会获得连接超时,因为呼叫将转到私有IP。

我在注册表中绑定服务,如下所示:

public class Server {
    private static final String SRVHOST = "<<external ip>>";
    private static Registry registry;

    public static void main(String[] args) {
        System.setProperty("java.rmi.server.hostname", SRVHOST);
        System.setProperty("java.rmi.server.useCodebaseOnly","false"); 
        System.setProperty("java.security.policy","src/srv.policy");

        if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());

        registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        ServerInterface services = new ServerImpl();
        registry.rebind("ServerAPI", services);
    }
}

客户获得服务:

public class Client {
    private static final String SRVHOST = "localhost";
    private static Registry registry;
    private static ClientInterface callback;

    public static void main(String[] args) {
        System.setProperty("java.security.policy", "src/cli.policy");

        if(System.getSecurityManager()==null)
            System.setSecurityManager(new RMISecurityManager());


        registry = LocateRegistry.getRegistry(SRVHOST, Registry.REGISTRY_PORT);
        ServerInterface services = (ServerInterface) registry.lookup("ServerAPI");
        callback = new ClientImpl();
        int nCallback = services.setClientInterface(callback);
        ...
        <<do_things>>
    }
}

方法setClientInterface(...)获取客户端对象并将其存储在ArrayList

我不知道是什么问题。有人能帮助我吗?

所有代码都在这里发布:

政策文件:

srv.policy - http://pastebin.com/LTsKSG3r

cli.policy - http://pastebin.com/zqxaRyT3

包:com.rmicallback.client

Client.java - http://pastebin.com/Xus04JZK

ClientImpl.java - http://pastebin.com/SdQXMDzs

ClientInterface.java - http://pastebin.com/S9PxT5vC

包:com.rmicallback.server

Server.java - http://pastebin.com/cxQcWy1Q

ServerImpl.java - http://pastebin.com/nqdGPbPr

ServerInterface.java - http://pastebin.com/nPxDJgs1

0 个答案:

没有答案