0

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

maxence5694
  • 3
  • 1
  • 3

1 Answers1

4

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

Ben Barkay
  • 5,473
  • 2
  • 20
  • 29
  • Thank you, this works. But now I have another error : `java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: java.rmi.AccessException: Registry.Registry.rebind disallowed; origin /192.168.1.56 is non-local host`. The origin is `theRegistry.bind(name, this)` in the client code. Can I register the client on the registry (which is on the server) ? So the server can call methods on the client. – maxence5694 Apr 04 '13 at 19:01
  • Looking at http://stackoverflow.com/questions/820719/using-single-rmi-registry it seem that you actually should use loopback for registry binding. Edited my answer aswell – Ben Barkay Apr 04 '13 at 19:10
  • I added the client registry binding in the registerClient method and it works well, thank you for your help. – maxence5694 Apr 04 '13 at 19:41