Bind address and port to RMI

2k Views Asked by At

I'm developing a chat, and firstly I was using sockets and streams to communicate between the server and client, which I already tested with my friends and it worked. Now I`m trying to convert to RMI.

So, it is working locally, but when my friend tries to connect he gets a refused connection. Weirder is that it shows the server local ip (192.168.25.28).

My code looks like this:

    public void conectar() throws RemoteException, MalformedURLException, NotBoundException{
    // COMUNICADOR SERVIDOR (USADO PELO CLIENTE)
    comunicadorServidor = new ComunicadorServidor(id); // cria um novo comunicador
    LocateRegistry.createRegistry(8081); // inicia o registro RMI na porta 8081
    Naming.rebind("//:8081/ComunicadorServidor", comunicadorServidor); // vincula o objeto comunicador ao endereço RMI
    // COMUNICADOR CLIENTE (USADO PELO SERVIDOR)
    comunicador = (IComunicadorCliente) Naming.lookup("//" + conexao.getInetAddress().getHostAddress() + ":8082/ComunicadorCliente"); // procura o comunicador no cliente
}

So I use RMI in both server and client, and one connect to each another, because in server I have methods like Authentication and Send Message, which the cliente uses, and in the client I have methods like Update Users List and Receive Message, which the server uses.

So first I create an instance of the RMI class (server-side), create a registry on port 8081, then bind the object to port 8081 with the name "ComunicadorServidor".

And after that the server tries to connect to the client RMI.

So I read about the rebind method, and if I don't specify an address, it will bind to the local host address, which I ask: even if it's binding to my local machine, is it still available outside? If not, how can I bind it in the same away as socket, where I just specify some port and it will be available to any address, like localhost, local ip, or external ip.

And about the lookup, what is the best way to get the client IP (right now I'm taking from the socket), how can I be sure that I will get a correct IP? Can I connect to the client RMI without a socket?

And in the client side it's essentialy the same thing, except it binds the RMI class to 8082 and lookup for the RMI class in server-side with the provided IP and port.

1

There are 1 best solutions below

2
On BEST ANSWER

Weirder is that it shows the server local ip (192.168.25.28).

That's correct. If you need it to appear on a public IP address you need to look up java.rmi.server.hostname, and see Item A.1 in the RMI FAQ.

So I read about the rebind method, and if I don't specify an address, it will bind to the local host address, which I ask: even if it's binding to my local machine, is it still available outside?

The Registry is available to the outside world if the server-side firewall rules so permit. Note that bind in the RMI sense just means associating a remote object with a name in the Registry: it has nothing to do with Socket.bind().

And about the lookup, what is the best way to get the client IP (right now I'm taking from the socket), how can I be sure that I will get a correct IP? Can I connect to the client RMI without a socket?

You can't 'connect' in RMI at all, but you don't need the client's IP address. The client should register its remote object with the server via a server remote method, e.g. register(Client client) throws RemoteException. Then the server has a stub on which it can call client remote methods directly (client-side firewall permitting).