Java世界

          學習筆記

          常用鏈接

          統計

          積分與排名

          天籟村

          新華網

          雅虎

          最新評論

          RMI遠程方法調用

          面向接口的編程。
          服務器端(定義接口,實現類)。
          用戶端(接口)。
          RMI調用。
          對象序列化。

          RMI的實現步驟:
          1.定義接口
          public interface RMIDemo extends Remote {
          ???????public String getString() throws RemoteException;

          }

          2.實現類
          public class RMIDemoImp extends UnicastRemoteObject implements RMIDemo {
          ???????public RMIDemoImp() throws RemoteException {
          ??
          ???????}
          ???????public String getString() throws RemoteException {
          ???????????return "hello world!";
          ???????}

          }

          3.打包(jar),配置到環境變量classpath中。

          4.打開RMI服務器(首先進入JDK目錄下的bin,再打開),rmiregistry 。

          5.對象注冊到服務器
          public class Server {
          ???????public static void main(String args[]) {
          ????????try {
          ????????????RMIDemoImp rmi = new RMIDemoImp();
          ????????????try {
          ????????????????Naming.bind("rmi",rmi);
          ????????????} catch (MalformedURLException e) {
          ?????????????// TODO Auto-generated catch block
          ????????????????e.printStackTrace();
          ????????????} catch (AlreadyBoundException e) {
          ?????????????// TODO Auto-generated catch block
          ????????????????e.printStackTrace();
          ????????????}
          ????????} catch (RemoteException e) {
          ?????????// TODO Auto-generated catch block
          ????????????e.printStackTrace();
          ????????}
          ????}

          }

          6.客戶端的建立
          public class Client {
          ????public static void main(String args[]) {
          ????????Object obj = java.rmi.Naming.lookup("rmi");
          ????????RMIDemo rmi = (RMIDemo)obj;
          ????????System.out.println(rmi.getString());
          ????}
          }

          posted on 2008-01-03 13:02 Rabbit 閱讀(195) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 灵璧县| 宁武县| 长乐市| 铜川市| 昭平县| 八宿县| 浦县| 峨眉山市| 五台县| 收藏| 射阳县| 扬中市| 方山县| 安庆市| 扶余县| 金平| 皮山县| 千阳县| 胶州市| 霍邱县| 叶城县| 梧州市| 双城市| 石泉县| 舞钢市| 辽中县| 永仁县| 沂源县| 延长县| 巢湖市| 遵义县| 博客| 青田县| 彭山县| 甘泉县| 兴海县| 庄河市| 绥化市| 乐平市| 芜湖县| 阳谷县|