javarmiconnectexception

Running Java RMI application on two machines - ConnectException


Long story short - I am trying to run an RMI application with a client and a server on separate machines using Windows.

The simplified code in the server is:

System.setProperty("java.rmi.server.hostname", "192.168.x.x");  
Registry reg = LocateRegistry.createRegistry(1099);  
RemoteFoo foo = new RemoteFoo();        
reg.rebind("Foo", foo);  

In the client I have:

reg = LocateRegistry.getRegistry("192.168.x.x", 1099);
RemoteFooInterface foo = (RemoteFooInterface) reg.lookup("Foo");

The exception I get is in "Connection refused to host: 192.168.x.x; nested exception is Connection timed out: connect" at the line where I look up the object.

I read some question on StackOverflow from people having a similar issue and that is why I added the line to change the System property in order to embed the correct IP in the stub that the clients use but it still does not work.
I will be very thankful if someone could provide me with some pointers on what else I can try.


Solution

  • It could be that firewall on the server is blocking access.

    Ensure that you can connect to the port, in your case, 1099. For example, from your client machine, open command prompt cmd.exe and type telnet 192.168.x.x 1099 (this will connect to port 1099 on the server). If it says 'Connecting To ...' then that port is not available and its very likely that client machine is unable to reach the server. Try ping 192.168.x.x (it will work if ICMP is allowed by server's firewall).

    Try fixing the firewall to allow connections through port 1099 or better disable it (temporarily) and give it a try again. Once cleared, run the RMI client