RMI无法连接到远程服务器 [英] RMI cannot connect to remote server

查看:130
本文介绍了RMI无法连接到远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用RMI,当我设法让它在locahost工作时,我在尝试使用远程服务器时遇到了各种各样的问题。这是我正在尝试运行的基本代码:

I've been plying with RMI recently and while I managed to make it work on locahost I've been having all sorts of problem when trying to use a remote server. Here's the basic code I'm trying to run:

服务器:

public class RmiServer extends UnicastRemoteObject implements RmiServerIntf {
    public static final String MESSAGE = "Hello world";

    public RmiServer() throws RemoteException {
    }

    public String getMessage() {
    return MESSAGE;
    }

    public static void main(String args[]) {
        System.out.println("RMI server started");

        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new RMISecurityManager());
            System.out.println("Security manager installed.");
            } else {
                   System.out.println("Security manager already exists.");
            }

            try {
                    LocateRegistry.createRegistry(1099);
                    System.out.println("java RMI registry created.");
            } catch (RemoteException e) {
                    e.printStackTrace();
            }

            try {
                    RmiServer obj = new RmiServer();

                    Naming.rebind("rmi://localhost/RmiServer", obj);

                    System.out.println("PeerServer bound in registry");
            } catch (Exception e) {
                    e.printStackTrace();
            }
      }
}

远程类接口:

public interface RmiServerIntf extends Remote {
    public String getMessage() throws RemoteException;
}

客户:

public class RmiClient { 
    RmiServerIntf obj = null; 

    public String getMessage() { 
        try { 
            obj = (RmiServerIntf)Naming.lookup("rmi://54.229.66.xxx/RmiServer");
            return obj.getMessage(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
            return e.getMessage();
        } 
    } 

    public static void main(String args[]) {
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new RMISecurityManager());
        }

        RmiClient cli = new RmiClient();

        System.out.println(cli.getMessage());
    }
}

rmi.policy文件:

rmi.policy file:

grant {
permission java.security.AllPermission;
};

我编译了类并为服务器创建了一个存根。然后,我将客户端,存根,接口和策略放在远程计算机上的计算机和服务器,存根,接口和策略上。作为Linux机器的远程服务器我使所有文件都可执行。我还在本地防火墙上添加了允许端口1099的规则,并打开了远程计算机上的所有端口

I compiled the classes and created a stub for the server. Then I placed client, stub, interface and policy on my machine and server, stub, interface and policy on the remote machine. The remote server being a Linux machine I made all the files executable. I also added a rule on the local firewall allowing port 1099, and opened all ports on the remote machine

之后我导航到远程计算机上的服务器目录并插入以下命令:

After this I navigated to the server's directory on the remote machine and inserted the following command:

java -Djava.security.policy=rmi.policy RmiServer

这不会给我带来问题所以我回到本地机器并输入

This didn't give me problems so I went back to the local machine and entered

java -Djava.security.policy=rmi.policy RmiClient

我等待,等待,我收到错误消息:

I wait, and wait and I get the error message:

Connection refused to host: 172.31.xx.xx; nested exception is: java.net.ConnectException: Connection timed out: connect 

我一直在与昨天整天这些连接错误,这是我得到的。我确定只有一个非常小的东西我仍然做错了但是我找不到它是什么。

I've been fighting with these connection errors all day yesterday and this is as far as I got. I'm sure there's only one very small thing I'm still doing wrong but I just can't find what it is.

推荐答案

这可能无法解决您的问题,但我在Linux上遇到过与JPPF(通过Java RMI)类似的问题。解决方案是确保客户端计算机上的短暂端口范围仅覆盖客户端本地防火墙允许的端口。例如,如果您的防火墙允许外部计算机连接端口48000到64000,请确保您的短暂端口范围也在48000到64000之间。尝试一下,让我们知道会发生什么。

This may not solve your problem, but I've had similar issues with JPPF (via Java RMI) on Linux. The solution was to ensure that the ephemeral port range on the Client-side machine covered only ports that were allowable by the Client-side's local firewall. E.g., if your firewall allows ports 48000 to 64000 to be connected to by an external machine, ensure that your ephemeral port range also falls within 48000 to 64000. Give that a try and let us know what happens.

这篇关于RMI无法连接到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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