互联网上的 Java rmi [英] Java rmi over the internet

查看:43
本文介绍了互联网上的 Java rmi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Java 中开发一款游戏,使用 RMI 进行所有网络通信.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.

我的客户端查找服务器(它的接口扩展 Remote)并在其上注册.它允许服务器知道谁连接了.我的客户还实现了一个扩展 Remote 的接口.这是我的代码的一部分:

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:

接口声明:

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)));

此解决方案在本地有效,但在我尝试通过 Internet 使用时无效.

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

我已设置我的路由器以将我的计算机暴露给固定 IP 地址并转发 1099 和 1100 端口.此解决方案允许其他开发人员查找"我的服务器并获得有效且可用的代理,但不允许他们导出他们的 IClient.似乎 JVM 试图将对象导出到他们的本地网络并且执行到此停止.

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.

因此,我尝试将 -Djava.rmi.server.hostname=my-external-IP JVM 参数传递给我的服务器本地和客户端远程.这样做,exportObject 会向远程 IP 而不是本地 IP 抛出 ConnectException.

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.

推荐答案

算了.在 Internet 上使用回调需要每个客户端配置他的防火墙/NAT 盒/任何东西以允许入站连接和可能的端口转发.其中许多根本无法配置,而且其中许多是由网络管理员运行的,他们只是不会这样做.

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.

这篇关于互联网上的 Java rmi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆