外部网络上的 RMI Chatprogram 推送配置不起作用

RMI Chatprogram push configuration on external network not working

我们正在尝试使用带有推送配置的 RMI 制作一个简单的聊天程序。 该程序在内部网络上运行,但是当我们尝试 运行 使用外部网络上的服务器运行该程序时,我们收到错误消息:

java.rmi.ConnectException: Connection refused to host: 192.168.2.24;
Caused by: java.net.ConnectException: Connection timed out: connect

客户端在接口'IChatServer'上调用方法'Broadcast(String s)'时出现错误 该方法位于服务端,调用服务端订阅的其他客户端。

我们的客户端可以连接到服务器。它可以从注册表获取绑定并从服务器调用方法。

但是当服务器试图从客户端调用方法时,我们得到这个错误。

服务器上转发1099端口,防火墙允许1099端口

有没有办法使这成为可能(使用 RMI)? 还是客户端的端口需要转发?

服务器:

  try {    
            String theIp = serverHostExternalIp;

            System.setProperty("java.rmi.server.hostname", theIp);

            //Implemented this so no random ports will be used
            RMISocketFactory.setSocketFactory(new FixedPortRMISocketFactory());

            registry = LocateRegistry.createRegistry(PORT_NUMBER);

            publisher = new RemotePublisher();
            publisher.registerProperty(BINDING_NAME);


            UnicastRemoteObject.unexportObject(server, true);
            UnicastRemoteObject.exportObject(server, PORT_NUMBER);

            UnicastRemoteObject.unexportObject(publisher, true);
            UnicastRemoteObject.exportObject(publisher, PORT_NUMBER);


            registry.rebind(BINDING_NAME, server);
            registry.rebind(PUBLISH_NAME, publisher);
        } catch (RemoteException ex) {
            System.err.println("[Server] Cannot bind student administration");
            System.err.println("[Server] RemoteException: " + ex.getMessage());
        }

IChat服务器:

public synchronized void tryConnect(String s, IChatClient client) throws RemoteException {        
    System.out.println("[Server] User connected: " + s);
}

public synchronized void broadcast(String s) throws RemoteException {
    // When this line is called, no errors occur, and the string is printed correctly.
    System.out.println("[Message] " + s);

    //on this line, the server tries to reach to all the clients (all listeners)
    //This line of code will generate an error.
    publisher.inform(BINDING_NAME, null, s);
}

客户:

    try {
                registry = LocateRegistry.getRegistry(ipAddress, PORT_NUMBER);            

                mycs = (IChatServer) registry.lookup(BINDING_NAME);

                //This method is located on the server and is called without errors.
                mycs.tryConnect(userid, this);                    

                publisher = (IRemotePublisherForListener) registry.lookup(PUBLISH_NAME);                    
                publisher.subscribeRemoteListener(this, BINDING_NAME);

            } catch (RemoteException ex) {
                System.err.println("[Client] Cannot lookup or subscribe publisher");
                System.err.println("[Client] RemoteException: " + ex.getMessage());
                registry = null;
            } catch (NotBoundException e) {
                System.err.println("[Client] Cannot lookup or subscribe publisher");
                System.err.println("[Client] NotBoundException: " + e.getMessage());
                registry = null;
            }

对于 RMI 方式,不要将端视为客户端-服务器,而是 remote servicecaller

因此 caller 需要能够通过 TCP 连接到 remote service 以执行远程方法调用 (RMI)。因此,在您的情况下,必须在两侧适当设置端口转发。因为这可能很麻烦(例如,客户端可能位于不受管理的 NAT 后面——就像你的情况一样),最好像 REST 服务一样威胁 RMI——所以只有一方在调用 remote service

除了转发 1099 RMI 注册表端口外,您还必须转发导出对象使用的端口。 RMI 注册表仅包含有关如何连接到实际导出的对象处理程序的信息。