gpt4 book ai didi

java - RMI 无法连接到远程服务器

转载 作者:行者123 更新时间:2023-12-01 06:24:22 25 4
gpt4 key购买 nike

我最近一直在使用 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();
}
}
}

远程类接口(interface):

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

我编译了这些类并为服务器创建了一个 stub 。然后,我将客户端、 stub 、接口(interface)和策略放置在我的计算机上,将服务器、 stub 、接口(interface)和策略放置在远程计算机上。远程服务器是一台 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 之内。尝试一下,让我们知道会发生什么。

关于java - RMI 无法连接到远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17681943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com