Java RMI Connect异常:连接拒绝主机/超时

Sam*_*ães 9 java rmi exception java-ee

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

java.rmi.ConnectException: Connection refused to host: 192.168.56.1; nested exception is: 
    java.net.ConnectException: Connection timed out: connect
Run Code Online (Sandbox Code Playgroud)

这是我的主要课程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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么建议?

Alb*_*ano 11

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

这个RMI 教程解释了:

在启动计算引擎之前,您需要启动RMI注册表.RMI注册表是一个简单的服务器端引导程序命名工具,它使远程客户端能够获取对初始远程对象的引用.

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

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

start rmiregistry
Run Code Online (Sandbox Code Playgroud)

Solaris OS或Linux:

rmiregistry &
Run Code Online (Sandbox Code Playgroud)

UPDATE

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

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

至:

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);
Run Code Online (Sandbox Code Playgroud)

因为教程报告:

静态UnicastRemoteObject.exportObject方法导出提供的远程对象,以便它可以从远程客户端接收其远程方法的调用.

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

Registry registry = LocateRegistry.getRegistry("localhost");
Run Code Online (Sandbox Code Playgroud)

只需调用:

Registry registry = LocateRegistry.getRegistry();
Run Code Online (Sandbox Code Playgroud)

因为Oracle报告:

LocateRegistry.getRegistry的无参数重载合成对本地主机和默认注册表端口1099上的注册表的引用.如果在1099以外的端口上创建注册表,则必须使用具有int参数的重载.

  • 一个防火墙的港口会 (2认同)