Search code examples
javaregistrylocalhostrmi

Java RMI connection to localhost at home network can't find correct remote module


I have been working on this project where two modules on different machines need to be in communication through RMI.

I start both client and server modules on my laptop. RMI seems to work correctly when i am at work and connected to work network, but when i am home, connected to my home network it does not work. It says remote object could not be found.

Here is the method i use at CLIENT side to get the reference to remote object

public static MyRMIApp getRemoteApp() throws RemoteException, NotBoundException, AccessException {
    Registry registry = LocateRegistry.getRegistry("localhost", 28999); // tried 127.0.0.1 instead of localhost here, still not working
    MyRMIApp app = (MyRMIApp) registry.lookup("COM");
    return app;
} 

Digging up a bit with some debugging, when i check the object value returned from getRemoteApp method, it shows me the end point is 67.215.65.132. Which is openDNS i am using to connect to internet. Shouldn't that be 127.0.0.1 ?

Then i used my mobile internet and tried again. It seems to be working but end-point is not 127.0.0.1 again it is the address assigned to me, which is 192.168.x.x

So can anybody please tell me what is wrong i am doing here ? I really would appreciate the help.

Oh and this is the piece of code at SERVER side

//Somwhere up top
private final static MyRMIApp rmiApp = new RMIServer();

//Down below
MyRMIApp stub = (MyRMIApp) UnicastRemoteObject.exportObject(rmiApp, 0);
        Registry registry = LocateRegistry.createRegistry(28999);
        registry.rebind("COM", stub);

Solution

  • See item A.1 of the RMI FAQ: specifically, 'The appropriate workaround is to set the system property java.rmi.server.hostname when starting the server.'