2017-10-27 23 views
0

rmi를 사용하여 서버 클라이언트 프로그램을 개발하고 있습니다. 2 Rmi의 서버 : 기본 및 BacIm 다른 서버에서 실행중인 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); 

문제는 여기에있다. 레지스트리 자체를 바인딩합니다. 이것은 말이되지 않습니다. 원격 객체 중 하나를 바인딩해야합니다.

+0

정말 고마워요, 정말 바보 같은 실수였습니다. 나는 그것이 옳았다는 이유로 의도하지 않게 그것을 변경했을 것입니다. 나는 틀린 것을 이해하지 못했기 때문에 이미 놀랐다. 다시 감사합니다. –