简体   繁体   中英

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; nested exception is: java.net.ConnectException: Connection timed out: connect

It seems that lookup function is searching at wrong network. I tried to use System.setProperty but it doesn't work. 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?

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. Doing it afterwards is too late.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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