测试您的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 OS或Linux:
rmiregistry &
UPDATE
我注意到,遵循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.