2017-10-27 6 views
0

rmiを使用してサーバークライアントプログラムを開発しています。私は2つのRmiのサーバーを持っています:プライマリとBac私は別のマシンで実行しているRmiServerにアクセスしようとしています。 2つの異なるマシン上でrmiにクライアントを接続しようとすると、最初はうまく動作し始めましたが、クラスのメソッドでいくつかの変更を加えた後、クライアントをサーバーに接続できなくなりました。私はあなたにサーバーとクライアントコードの抜粋を示します。Java RMI - RegistryImpl_Stubをリモートインタフェースにキャストできません

サーバー側

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

インタフェースConnectionRMI

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 

答えて

0
registry.bind("RmiServer1", registry); 

問題はここにあります。レジストリをそれ自身にバインドしています。これは意味がありません。リモートオブジェクトの1つをバインドする必要があります。

+0

本当にありがとう、それは本当に非常にばかげた過ちでした。私はそれが正しかったので、それを意図せずに変更したに違いありません。私は何が間違っているのか分からなかったので、私はすでに怒っていました。ありがとうございました。 –

関連する問題