龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

Java RMI编程

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
bStep1:ImplementstheinterfaceofRemoteServerasSimpleCounterServer.Java/b publicinterfaceSimpleCounterServerextendsjava.rmi.Remote { publicintgetCount()throwsjava.rmi.RemoteException; } CompileitwithjavacSimpleCounterServer.java bStep2:ProdUC

  <b>Step 1: Implements  the  interface of Remote Server as SimpleCounterServer.Java</b>
  
      public interface SimpleCounterServer extends java.rmi.Remote
  {
      public int getCount() throws java.rmi.RemoteException;
      
  }
  Compile it with javac SimpleCounterServer.java
  
  <b>Step  2:      ProdUCe the implement file SimpleCounterServerImpl.java as</b>
  import java.rmi.*;
  import java.rmi.server.UnicastRemoteObject;
  //
  //
  // SimpleCounterServerImpl
  //
  //
  public class SimpleCounterServerImpl
  extends UnicastRemoteObject
  implements SimpleCounterServer
  {
      private int iCount;
      public SimpleCounterServerImpl() throws java.rmi.RemoteException
      {
          iCount = 0;
      }
      public int getCount() throws RemoteException
      {
          return ++iCount;
      }
  
      public static void main(String args[])
      {
          System.setSecurityManager(new RMISecurityManager());
          try
          {
              SimpleCounterServerImpl server = new SimpleCounterServerImpl();
              System.out.println("SimpleCounterServer created");
              Naming.rebind("SimpleCounterServer",server);
              System.out.println("SimpleCounterServer registered");
          }
          catch(RemoteException x)
          {
              x.printStackTrace();
          }        
  
精彩图集

赞助商链接