简体   繁体   English

互联网上的Java RMI连接

[英]Java RMI connection over internet

I'm trying to get remote objects from a server hosted on different network. 我正在尝试从不同网络上托管的服务器获取远程对象。 I'm able to connect on same machine and on same network, but when I try to get it from different network I get: 我可以在同一台机器和同一网络上进行连接,但是当我尝试从不同的网络获得它时,我得到:

Connection refused to host: 192.168.1.131; 连接拒绝主机:192.168.1.131; nested exception is: java.net.ConnectException: Connection timed out: connect 嵌套的异常是:java.net.ConnectException:连接超时:connect

It seems that lookup function is searching at wrong network. 查找功能似乎正在搜索错误的网络。 I tried to use System.setProperty but it doesn't work. 我尝试使用System.setProperty,但是它不起作用。 Here the code: 这里的代码:

Server 服务器

 public class Main {

    public static void main(String[] args) {
        try{
            System.out.println("Init server...\n");
            TestInterface test = new TestImplement();


            System.setProperty("java.rmi.server.hostname", "95.247.x.x");
            System.out.println("Reg RMI...\n");
            Registry rmiRegistry = LocateRegistry.createRegistry(5555);
            rmiRegistry.rebind("Test" , test);
            System.out.println("Reg completed!\n");
            }catch(Exception e){
                e.printStackTrace();
            }
        }

}

Client 客户

...
registryRMI = LocateRegistry.getRegistry("95.247.x.x",5555);
TestInterface testClient = (TestInterface)registryRMI.lookup("Test");
...

Do I need to set java.rmi.server.hostname in client jar as well? 我是否还需要在客户端jar中设置java.rmi.server.hostname

TestInterface test = new TestImplement();
System.setProperty("java.rmi.server.hostname", "95.247.x.x");

You need to set java.rmi.server.hostname before exporting any remote objects. 导出任何远程对象之前,需要设置java.rmi.server.hostname Doing it afterwards is too late. 之后再做为时已晚。

System.setProperty("java.rmi.server.hostname", "95.247.x.x");
TestInterface test = new TestImplement();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM