Java RMI Connect Exception: Connection refused to host / timeout
Test if your 1099 port is available (that means blocked by firewall). Also, you didn't mentioned what OS you are using and if you started the registry before the execution of your server.
This RMI tutorial explains:
Before starting the compute engine, you need to start the RMI registry. The RMI registry is a simple server-side bootstrap naming facility that enables remote clients to obtain a reference to an initial remote object.
By default, the registry runs on port 1099, like yours. As the tutorial reports, just open a command prompt (on Windows) or a shell terminal (on a UNIX-like OS) and type:
For Windows (use javaw if start is not available):
start rmiregistry
Solaris OS or Linux:
rmiregistry &
UPDATE
I noticed, following the Oracle's tutorial and a my project of time ago, that in the Server
class, you didn't exported the object to the RMI runtime.
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);
to:
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 static UnicastRemoteObject.exportObject method exports the supplied remote object so that it can receive invocations of its remote methods from remote clients.
Also, if you are using the same host for a RMI invocation, it isn't needed in the Client
class:
Registry registry = LocateRegistry.getRegistry("localhost");
Simply invoke:
Registry registry = LocateRegistry.getRegistry();
Because Oracle reports:
The no-argument overload of LocateRegistry.getRegistry synthesizes a reference to a registry on the local host and on the default registry port, 1099. You must use an overload that has an int parameter if the registry is created on a port other than 1099.
Samuel Guimarães
Updated on July 09, 2022Comments
-
Samuel Guimarães almost 2 years
I'm working on a RMI command-line game but, whenever I try to my Service, I receive an error like this:
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()); } } }
The main 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?
-
user207421 over 12 yearsNone of this would account for a connection timeout.
-
Alberto Solano over 12 years@EJP About connection timeout, I wrote: "Test if your 1099 port is available". This would mean to check if the port is available or firewalled. My note about the starting of rmiregistry is because the OP didn't written anything about that, and because, sometimes the non-execution of the service is the reason of a connection refused. If the Client wants to execute a method of Server, but if the Server didn't run locally with the service, the Client will never execute the method.
-
Samuel Guimarães over 12 yearsI was testing this on a Mac and also on Windows 7. Also tried on a Win XP virtual machine with no Av / Firewalls, got the same results. I've changed getRegistry("localhost") to getRegistry("localhost",1099) and now I received a java.rmi.UnmarshalException: error unmarshalling return; nested exception is: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException
-
Alberto Solano over 12 years@Sprite I understood. Following the Oracle's tutorial and reviewing your code, in Server class, you didn't done the export to the RMI runtime of your Controle object (called "obj") with "Controle stub = (Controle) UnicastRemoteObject.exportObject(obj, 0);" before the Registry creation. Then, you should edit the line "reg.rebind("CtrlServ", obj);" to "reg.rebind("CtrlServ", stub);"
-
user207421 over 12 yearsNone of this is relevant. The OP is calling
LocateRegistry.createRegistry(1099)
, so: (a) he doesn't need to start it at the command line; (b) he doesn't need to worry about the port not being 1099; and (c) the port is available, otherwise he would be getting a BindException wrapped in an ExportException. Non-execution of a service doesn't cause 'connection timeout'; neither does non-exporting of a remote object; and you only have to callexportObject()
if your class doesn't extendUnicastRemoteObject
. -
Alberto Solano about 7 years@horseatingweeds There's plenty of method for checking it. You may have a look at serverfault.com/questions/35218/…
-
truthadjustr over 4 yearsAdd this statement in the main of your
Server.java
file:System.setProperty("java.rmi.server.hostname","192.168.143.69");
wherein192.168.143.69
is the server's ip address.