2017-12-15 25 views
-2

나는 두 개의 자바 클래스를 가지고 있는데 하나는 클라이언트이고 다른 하나는 서버이다. 클라이언트는 서버 IP 주소를 주어야한다.하지만 나는 다이나믹하게 만들고 싶다. 클라이언트는 DNS와 같은 메소드를 검색하고이 메소드를 제공하는 특정 서버의 IP 주소를 리턴하는 세 번째 시스템 (이름 지정 시스템)의 IP를 알고 있습니다.다른 컴퓨터에서 namming system witj rmi하는 방법

편집 :

CLASSE 클라이언트 :

public class Client { 
private Client() {} 

public static void main(String[] args) { 

    String host = (args.length < 1) ? null : args[0]; 
    try { 
     //Registry registry = LocateRegistry.getRegistry(host); 
     Registry registry = LocateRegistry.getRegistry("192.168.1.9",1091); 

     Calculator stub = (Calculator) registry.lookup("Hello"); 

     String response = stub.add(4,2); 
     System.out.println("response: " + response); 
    } catch (Exception e) { 
     System.err.println("Client exception: " + e.toString()); 
     e.printStackTrace(); 
    } 
} 

}

서버 :

public class Server implements Calculator{ 

public Server() {} 

public String add(int a,int b) { 
    return "Hello, a+b= "+(a+b); 
} 
public String sub(int a,int b) { 
    return "Hello, a-b= "+(a-b); 
} 
public static void main(String args[]) { 

    try { 
     Server obj = new Server(); 
     Calculator stub = (Calculator) UnicastRemoteObject.exportObject(obj, 0); 

     // Bind the remote object's stub in the registry 

     Registry registry = LocateRegistry.createRegistry(1091); 
     registry.bind("Hello", stub); 

     System.err.println("Server ready"); 
    } catch (Exception e) { 
     System.err.println("Server exception: " + e.toString()); 
     e.printStackTrace(); 
    } 
} 

}

+1

코드 또는 지금까지 시도한 것을 제공해주십시오. – Yannis

+0

나는이 질문에 대해 '너무 광범위하다'라는 것을 모른다. – EJP

답변

0

당신은 RMI 레지스트리에이 작업을 수행 할 수 없습니다. 레지스트리와 동일한 호스트에서 실행중인 프로세스 만 바인딩 할 수 있습니다. LDAP와 같은 다른 이름 지정 서비스를 사용해야합니다.

그러나 나머지 설명은 RMI가 이미 수행 한 것입니다. 레지스트리에서 이름을 찾아 보면 해당 원격 객체와 통신하는 방법을 알고있는 스텁을 반환합니다.

+0

그래서 LDAP를 구현하고 별도의 컴퓨터에 넣어 클라이언트가 주소를 지정하고 클라이언트의 서버 IP 주소를 올바르게 반환합니다. –

+0

그보다 간단합니다. LDAP 서버를 레지스트리로 사용하기 만하면됩니다. 원격 객체를 LDAP 서버에 바인드합니다. 클라이언트가 LDAP 서버를 검색하여 스텁을 가져온 다음 remone 메소드를 호출하십시오. RMI가 IP 주소를 처리합니다. – EJP