Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java RMI Connect Exception: Connection refused to host / timeout

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?

like image 969
Samuel Guimarães Avatar asked Dec 13 '11 06:12

Samuel Guimarães


People also ask

What is Java RMI server hostname?

hostname. RMI uses IP addresses to indicate the location of a server (embedded in a remote reference). If the use of a hostname is desired, this property is used to specify the fully-qualified hostname for RMI to use for remote objects exported to the local JVM. The property can also be set to an IP address.

How do I run an RMI program in eclipse?

rmi and select PurseImpl. java . From the Eclipse menu bar, select Run, then External Tools and rmic. (If this is the first rmic execution, select Run, External Tools , External Tools Configurations... and click Run).

What is RMI in Java with example?

RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM. RMI is used to build distributed applications; it provides remote communication between Java programs. It is provided in the package java.


1 Answers

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.

like image 144
Alberto Solano Avatar answered Nov 11 '22 07:11

Alberto Solano