我正在用 Java 开发一个游戏,使用 RMI 进行所有网络通信。 RMI 允许我调用服务器上的方法,但这对我来说还不够。我还希望服务器能够在连接的客户端之间传播消息。
我的客户端查找服务器(它的接口扩展了远程)并在其上注册。它允许服务器知道谁连接了。我的客户还实现了一个扩展 Remote 的接口。这是我的代码的一部分:
接口声明:
public interface IServer extends Remote {
void connect(IClient Client) throws RemoteException, ExistingItemException;
//...
}
public interface IClient extends Remote {
public void notify(Notification Notification) throws RemoteException;
//...
}
服务器端:
//int RMIPort = 1099, ServerPort = 1100;
IServer Server = new RMIServer();
IServer Proxy = (IServer) UnicastRemoteObject.exportObject(Server, ServerPort);
LocateRegistry.createRegistry(RMIPort).rebind("//" + LocalIP + ":" +
RMIPort + "/xxx", Proxy);
客户端:
//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)));
该解决方案在本地有效,但当我尝试通过互联网使用它时却无效。
我已经设置了路由器,将我的计算机暴露给固定 IP 地址并转发 1099 和 1100 端口。此解决方案允许其他开发人员“查找”我的服务器并获取有效且可用的代理,但不允许他们导出其 IClient。看起来 JVM 试图将对象导出到本地网络,并且执行在此停止。
所以,我试图通过-Djava.rmi.server.hostname=my-external-IP
JVM 参数对于我的服务器来说是本地的,对于客户端来说是远程的。这样做时,exportObject 会抛出一个ConnectException
到远程ip而不是本地ip。