1

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?

Balasubramanian
  • 700
  • 7
  • 26
TheEnigmist
  • 906
  • 1
  • 10
  • 23
  • 1
    Check if you are able to telnet on 5555 port. – mkalsi Sep 19 '17 at 14:22
  • as well the question is why the client tries to connect `192.168.1.131` .. maybe the registry binds the instance to that address.. did yo urestart it too? – gusto2 Sep 19 '17 at 14:58
  • @gusto2 'Registry binds the instance to that address' is completely meaningless. The *application* binds the *name* to the *instance* via the *Registry*. There is no notion of address in the `Registry.bind()` operation. – user207421 Sep 21 '17 at 09:30
  • Not a duplicate of https://stackoverflow.com/questions/5907037/connection-timed-out-why as this is asking how to configure RIM to avoid such problems. – Raedwald Nov 08 '17 at 15:08

1 Answers1

0
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();
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Ok now the client get the proper IP, but the connection is still timed out. "Connection refused to host: 95.247.x.x; nested exception is: java.net.ConnectException: Connection timed out: connect" I done port forwarding in my router so it maps external port to internal port correctly. Still getting this error. – TheEnigmist Sep 22 '17 at 11:08
  • No, you are now getting a *new* error. There is no 'still' about it. The problem is now that you haven't done the port forwarding correctly, or that 95.247.x.x is behind a firewall, or some other network connectivity problem. – user207421 Sep 23 '17 at 00:53
  • The problem is not the firewall becouse if I set on server `java.rmi.server.hostname "blablabla"` on client I get `java.net.UnknownHostException: blablabla` so the client is able to retrive the registry or at least first connection, after that it can't progress for connection timed out. – TheEnigmist Sep 23 '17 at 09:28