Java RMI - RegistryImpl_Stub 无法转换为我的远程接口

Java RMI - RegistryImpl_Stub cannot be cast to my remote interface

我正在使用 rmi 开发服务器-客户端程序。我有 2 个 Rmi 的服务器:主服务器和 Bac 我正在尝试访问另一台机器上的 RmiServer 运行。在我第一次尝试将客户端连接到 2 台不同机器上的 rmi 时,它开始工作正常,但在修改 类 的方法中的一些内容后,我无法将客户端连接到服务器。我将向您展示服务器和客户端代码的摘录。

服务器端

public class RmiServer extends UnicastRemoteObject implements ConnectionRMI

//MAIN Function

try{
    if(args.length!=4){
        System.out.println("Number of arguments invalid! Rmi Server will close.");
        return;
       }
        address = InetAddress.getByName(args[0]);
        portUdp = Integer.parseInt(args[1]);
        otherPort = Integer.parseInt(args[2]);
        otherAddress = InetAddress.getByName(args[3]);
        Registry registry = LocateRegistry.getRegistry(otherAddress.getHostAddress(), 1099);
                registry.lookup("RmiServer1");
                System.out.println("[INFO]: Backup Server RMI is ready! ");
                primary = false;
            } catch (NotBoundException | RemoteException ex) {
                primary = true;
                try {
                    Registry registry = LocateRegistry.createRegistry(1099);
                    registry.rebind("RmiServer1", registry);
                } catch (RemoteException ex1) {
                    System.out.println("The registry in port 1099 is already created. The program will close\n");
                    return;
                }
                System.out.println("[INFO]: Primary Server RMI is ready! ");

            } catch (UnknownHostException ex) {
                Logger.getLogger(RmiServer.class.getName()).log(Level.SEVERE, null, ex);
            }

接口连接RMI

public interface ConnectionRMI extends Remote{
}

客户端

try{
            address1 = InetAddress.getByName(args[2]);
            address2 = InetAddress.getByName(args[3]);
            registry = LocateRegistry.getRegistry(address1.getHostAddress(), 1099);
            atualRmi = (ConnectionRMI) registry.lookup("RmiServer1");
            server1 = true;
        }

错误在这一行:

atualRmi = (ConnectionRMI) registry.lookup("RmiServer1");

错误:

Exception in thread "main" java.lang.ClassCastException: sun.rmi.registry.RegistryImpl_Stub cannot be cast to sdeleicoes.ConnectionRMI
registry.bind("RmiServer1", registry);

问题就在这里。您正在将注册表绑定到自身。这是没有意义的。您应该绑定您的远程对象之一。