Java RMI Connect exception: connection refused host / timeout

I work on the RMI command line, but whenever I try to execute my service, I get this error:

java.rmi.ConnectException: Connection refused to host: 192.168.56.1; nested exception is: java.net.ConnectException: Connection timed out: connect 

This is the main class for my Server :

 public class RMIWar { public static void main(String[] args) throws RemoteException, MalformedURLException { try { Controle obj = new Controle(4); Registry reg = LocateRegistry.createRegistry(1099); System.out.println("Server is ready"); reg.rebind("CtrlServ", obj); } catch (Exception e) { System.out.println("Error: " + e.toString()); } } } 

Basic for my Client class:

 public class RMIWarClient { public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException { try { Registry registry = LocateRegistry.getRegistry("localhost"); ControleInt ctrl = (ControleInt) registry.lookup("CtrlServ"); System.out.println("CtrlServ found...\n"); BioRMI bio = new BioRMI(null, 5,5,5); ctrl.thRegister("Test", bio.atk, bio.def, bio.agi); } catch (Exception e) { System.out.println("Error: " + e); } } } 

Any suggestions?

+9
source share
4 answers

Check if your port 1099 is accessible (this means that it is blocked by a firewall). In addition, you did not mention which OS you are using, and whether you ran the registry before starting your server.

This RMI tutorial explains:

Before starting the computing engine, you need to run the RMI registry. The RMI registry is a simple server-side bootstrap naming facility that allows remote clients to get a link to the source remote object.

By default, the registry runs on port 1099, as you have. As the tutorial reports, simply open a command prompt (on Windows) or a shell terminal (on a UNIX-like OS) and enter:

For Windows (use javaw if launch is not available):

 start rmiregistry 

Solaris or Linux OS :

 rmiregistry & 

UPDATE

After training Oracle and my project at that time, I noticed that in the Server class you did not export the object at runtime RMI. Then you should edit these lines:

 Controle obj = new Controle(4); Registry reg = LocateRegistry.createRegistry(1099); System.out.println("Server is ready"); reg.rebind("CtrlServ", obj); 

so that:

 Controle obj = new Controle(4); Controle stub = (Controle) UnicastRemoteObject.exportObject(obj, 0); Registry reg = LocateRegistry.createRegistry(1099); System.out.println("Server is ready"); reg.rebind("CtrlServ", stub); 

Because the tutorial reports:

The UnicastRemoteObject.exportObject static method exports the provided remote object so that it can receive calls to its remote methods from remote clients.

In addition, if you use the same host to call RMI, it is not needed in the Client class:

 Registry registry = LocateRegistry.getRegistry("localhost"); 

Just call:

 Registry registry = LocateRegistry.getRegistry(); 

Because Oracle reports:

Overload with no arguments LocateRegistry.getRegistry synthesizes a registry link on the local host and on the default registry port, 1099. You must use the overload with the int parameter if the registry is created on a port other than 1099.

+11
source

Be careful with the ports, because they can be blocked for the firewall, for example, if you work in a protected area, for example, in your work, or at the university, the administrator might implement some protections ...

0
source

I do not see how you can get this exception from this code. According to the code, your server and client work on the same host, and you do not receive connection timeouts on the same host.

So, assuming it is not your real code, and you are looking for the correct registry ( getRegistry() host argument), I would ask if 192.168.56.1 is the IP address you expect to see, given your network topology. This is the internal address of the local network, and if your client is located outside the local network, he should try to connect to a more public IP address. If the address is not what you expect, I refer you to clause A.1 in the RMI FAQ .

0
source

I described my solution here java.rmi.ConnectException: the connection refused the host: 127.0.1.1; and here Can not connect to MBeanServer Tomcat via jconsole in Java6 . The problem was the main network problem, but it could happen to someone else.

0
source

All Articles