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?
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:
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):
Solaris OS or Linux:
UPDATE
I noticed, following the Oracle’s tutorial and a my project of time ago, that in the
Serverclass, you didn’t exported the object to the RMI runtime.Then you should edit these lines:
to:
Because the tutorial reports:
Also, if you are using the same host for a RMI invocation, it isn’t needed in the
Clientclass:Simply invoke:
Because Oracle reports: