Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java rmi over the internet

Tags:

java

rmi

I am developing a game in Java using RMI for all network communication. RMI allows me to call method on my server, but it is not enough for me. I also want the server to be able to spread message among connected clients.

My clients look up for the server (it's interface extends Remote) and register on it. It allows the server to know who is connected. My clients also implement an interface that extends Remote. Here is some part of my code:

Interfaces declaration:

public interface IServer extends Remote {
    void connect(IClient Client) throws RemoteException, ExistingItemException;
    //...
}

public interface IClient extends Remote {
    public void notify(Notification Notification) throws RemoteException;
    //...
}

Server side:

//int RMIPort = 1099, ServerPort = 1100;
IServer Server = new RMIServer();
IServer Proxy = (IServer) UnicastRemoteObject.exportObject(Server, ServerPort);
LocateRegistry.createRegistry(RMIPort).rebind("//" + LocalIP + ":" + 
        RMIPort + "/xxx", Proxy);

Client side:

//Sets the local reference to IServer and creates the IClient
setInstance(new Client(Login, (IServer) LocateRegistry.getRegistry(RemoteIP).
        lookup("//" + RemoteIP + ":" + RMIPort + "/xxx")));
//Gets the IClient and makes it available for the IServer to call notify(...)
Proxy.connect((IClient) (UnicastRemoteObject.exportObject(getInstance(), 0)));

This solution works in local but doesn't when I'm trying to use it through Internet.

I have setup my router to expose my computer to a fix IP address and forward the 1099 and 1100 ports. This solution allow other developers to "lookup" my server and get a valid and usable Proxy, but it doesn't allow them to export their IClient. It seems that the JVM tries to export the object to their local network and the execution stops here.

So, I have tried to pass -Djava.rmi.server.hostname=my-external-IP JVM argument both local for my server and remote for clients. Doing so, exportObject throws a ConnectException to the remote ip instead of local one.

like image 216
Florent Henry Avatar asked May 14 '26 02:05

Florent Henry


2 Answers

Forget it. Using callbacks over the Internet requires every client to configure his firewall/NAT box/whatever to allow inbound connections and probable port forwarding. Many of them aren't configurable at all, and many of them are run by net admins who just won't do it.

NB you would also have to export on a fixed port.

like image 81
user207421 Avatar answered May 15 '26 14:05

user207421


There is an alternative:

http://dev.root1.de/projects/simon/wiki#Why-SIMON-is-better-than-

The protocol is not compatible to RMI, but the usage is almost the same. Changing from RMI to SIMON is typically not that hard. I heared from users that switching to SIMON took just 30min.

like image 35
Alex Avatar answered May 15 '26 16:05

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!