javanetwork-programmingrmilanrmiregistry

RMI - work in the same machine, not in LAN


I am trying to establish two-communication between one server and two clients. This works very well when all programs run on the same machine but it doesn't work when I try using LAN network. I got the error :

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

Here is the server code :

public class Server{
private Game partie; // The class Game extends UnicastRemoteObject and implements ServerInterface


public Server() throws RemoteException {
    System.setProperty("java.rmi.server.hostname", "192.168.1.24");
    partie = new Game();
    LocateRegistry.createRegistry(1099);
    try{
        Naming.rebind("Server", partie);
    }
    catch(Exception e){
        e.printStackTrace();
    }
}
public static void main(String argv[]) throws RemoteException{
    new Server();
}
}

Here is the constructor of the client code :

public Client(String aName, String aServerAdress) throws RemoteException {
    super();
    name = aName;
    ServerAdress = aServerAdress; //  = "192.168.1.24"
    theRegistry = LocateRegistry.getRegistry(ServerAdress);

    try {
        serverInterface = (ServerInterface) theRegistry.lookup("Server");
    } catch (NotBoundException e1) {
        e1.printStackTrace();
    }



    try {
        theRegistry.bind(name, this); // For two-way communication
    } catch (AlreadyBoundException e) {
        e.printStackTrace();
    }


    serverInterface.registerClient(name);
}

Where registerClient(String name) code is approximately (in Game class) :

cd_client = (ClientInterface) Naming.lookup("rmi://127.0.0.1/" + name);

All firewalls are disabled.

I have been working on this problems for many hours and I have still not found what is wrong. I would really appreciate if you could help me a bit. Thank you


Solution

  • Change all occurances of 127.0.0.1 (except registry binding) to your LAN IP address (192.168.1.24 in your case)

    127.0.0.1 is a Loopback address:

    "Loopback (loop-back) describes ways of routing electronic signals, digital data streams, or flows of items from their originating facility back to the receiving end of the source without intentional processing or modification. This is primarily a means of testing the transmission or transportation infrastructure." -- from Wikipedia