1.Remote を継承したインターフェイスを作成 2.作成したインターフェイスを実装したクラスを作成し、処理を書く 3.サーバは2のインスタンスを登録 4.クライアントはサーバに登録されているオブジェクトを1のインターフェイスでアクセス {{code Java, import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.UnicastRemoteObject; // class RemoteObject extends UnicastRemoteObject implements Remote { // class RemoteInterface extends UnicastRemoteObject implements Remote { interface RemoteInterface extends Remote { public String test(String a, String b) throws RemoteException; } // class RemoteObject extends RemoteInterface { class RemoteObject extends UnicastRemoteObject implements RemoteInterface { public RemoteObject() throws RemoteException { super(); } public String test(String a, String b){ return a + b; } } public class RMIServer { public static void main(String[] args){ try{ Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT); RemoteObject ro = new RemoteObject(); Naming.rebind("RMIServer", ro);//リモートオブジェクトに名前をつける // Naming.rebind("//localhost/RMIServer", ro);//リモートオブジェクトに名前をつける System.out.println("RMIServer bound in registry"); } catch(Exception e){ e.printStackTrace(); } } } }} {{code Java, import java.rmi.*; public class RMIClient { public static void main(String[] args){ String remotehost = "localhost"; // if (args.length < 1){ // System.out.println("Usage: java RMIClient [remotehost]"); // System.exit(1); // } if(args.length == 1){ remotehost = args[0]; } try{ RemoteInterface ro = (RemoteInterface)Naming.lookup("//"+remotehost+"/RMIServer"); System.out.println(ro.test("hoge","test")); System.out.println(Naming.lookup("//"+remotehost+"/RMIServer").getClass()); } catch(Exception e){ e.printStackTrace(); } } } }} {{category2 プログラミング言語,Java}}