RMI无法连接到远程服务器

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

服务器:

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文件:

 grant { permission java.security.AllPermission; }; 

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

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

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

这没有给我带来问题所以我回到了本地机器并进入了

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

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

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

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

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

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

请在RMIServer端代码中使用上述语句,然后再次尝试从远程客户端连接。 它对我有用