11

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?

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
Samuel Guimarães
  • 574
  • 3
  • 6
  • 18

4 Answers4

13

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.

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
  • None of this would account for a connection timeout. – user207421 Dec 13 '11 at 09:45
  • @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. – Alberto Solano Dec 13 '11 at 11:42
  • I 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 – Samuel Guimarães Dec 13 '11 at 11:47
  • @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);" – Alberto Solano Dec 13 '11 at 12:07
  • None 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 call `exportObject()` if your class doesn't extend `UnicastRemoteObject`. – user207421 Dec 13 '11 at 21:37
  • @horseatingweeds There's plenty of method for checking it. You may have a look at http://serverfault.com/questions/35218/in-windows-using-the-command-line-how-do-you-check-if-a-remote-port-is-open – Alberto Solano Apr 03 '17 at 11:51
  • Add this statement in the main of your `Server.java` file: `System.setProperty("java.rmi.server.hostname","192.168.143.69");` wherein `192.168.143.69` is the server's ip address. – daparic Oct 21 '19 at 12:17
0

I described my solution here java.rmi.ConnectException: Connection refused to host: 127.0.1.1; and here Cannot connect to Tomcat's MBeanServer via jconsole in Java6. the issue was a basic networking issue, but it might happen to someone else.

Community
  • 1
  • 1
Paul
  • 7,155
  • 8
  • 41
  • 40
0

Be careful about the ports cause they could be blocked for a firewall, for example if you are working on a secure area like you work or the university, the administrator could have implemented some protections...

melli-182
  • 1,216
  • 3
  • 16
  • 29
0

I don't see how you can possibly be getting that exception from that code. According to the code your server and client are running on the same host, and you don't get connection timeouts within the same host.

So assuming that's not your real code and you are looking up the correct registry (host argument to getRegistry()), I would ask whether 192.168.56.1 is the IP address you expect to see given your network topology. It's an internal LAN address and if your client is outside the LAN it should be trying to connect to a more public IP address. If the address isn't what you expect I refer you to item A.1 on the RMI FAQ.

user207421
  • 305,947
  • 44
  • 307
  • 483