gpt4 book ai didi

Java RMI 连接异常 : Connection refused to host/timeout

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:16:35 25 4
gpt4 key购买 nike

我正在开发一个 RMI 命令行游戏,但是每当我尝试使用我的服务时,我都会收到这样的错误:

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

这是我的 Server 的主类:

public class RMIWar {

public static void main(String[] args) throws RemoteException, MalformedURLException {
try {
Controle obj = new Controle(4);
Registry reg = LocateRegistry.createRegistry(1099);
System.out.println("Server is ready");
reg.rebind("CtrlServ", obj);
}
catch (Exception e) {
System.out.println("Error: " + e.toString());
}
}
}

我的 Client 类的主要部分:

public class RMIWarClient {
public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException {
try {
Registry registry = LocateRegistry.getRegistry("localhost");
ControleInt ctrl = (ControleInt) registry.lookup("CtrlServ");
System.out.println("CtrlServ found...\n");
BioRMI bio = new BioRMI(null, 5,5,5);
ctrl.thRegister("Test", bio.atk, bio.def, bio.agi);

}

catch (Exception e) {
System.out.println("Error: " + e);
}
}
}

有什么建议吗?

最佳答案

测试您的 1099 端口是否可用(这意味着被防火墙阻止)。此外,您没有提到您使用的是什么操作系统,以及您是否在执行服务器之前启动了注册表。

此 RMI tutorial解释:

Before starting the compute engine, you need to start the RMI registry. The RMI registry is a simple server-side bootstrap naming facility that enables remote clients to obtain a reference to an initial remote object.

默认情况下,注册表在端口 1099 上运行,就像您的一样。如教程所述,只需打开命令提示符(在 Windows 上)或 shell 终端(在类 UNIX 操作系统上)并键入:

对于 Windows(如果启动不可用,请使用 javaw):

start rmiregistry

Solaris 操作系统或 Linux:

rmiregistry &

更新

我注意到,按照 Oracle 的教程和我之前的一个项目,在 Server 类中,您没有将对象导出到 RMI 运行时。然后你应该编辑这些行:

Controle obj = new Controle(4);
Registry reg = LocateRegistry.createRegistry(1099);
System.out.println("Server is ready");
reg.rebind("CtrlServ", obj);

到:

Controle obj = new Controle(4);
Controle stub = (Controle) UnicastRemoteObject.exportObject(obj, 0);
Registry reg = LocateRegistry.createRegistry(1099);
System.out.println("Server is ready");
reg.rebind("CtrlServ", stub);

因为教程报告:

The static UnicastRemoteObject.exportObject method exports the supplied remote object so that it can receive invocations of its remote methods from remote clients.

此外,如果您使用同一主机进行 RMI 调用,则在 Client 类中不需要它:

Registry registry = LocateRegistry.getRegistry("localhost");

只需调用:

Registry registry = LocateRegistry.getRegistry();

因为 Oracle 报告:

The no-argument overload of LocateRegistry.getRegistry synthesizes a reference to a registry on the local host and on the default registry port, 1099. You must use an overload that has an int parameter if the registry is created on a port other than 1099.

关于Java RMI 连接异常 : Connection refused to host/timeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8485239/

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