1 //////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 2 /////////////////////////////// RMIcount.java \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 3 //////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 4 import java.rmi.*; 5 interface RMIcount extends Remote { 6 void add( int in ) throws RemoteException; 7 int get() throws RemoteException; 8 } 9 ///////////////////////////// RMIcountImpl.java \\\\\\\\\\\\\\\\\\\\\\\\\\\\ 10 import java.rmi.*; 11 import java.rmi.server.*; 12 public class RMIcountImpl extends UnicastRemoteObject 13 implements RMIcount { 14 private int total; 15 public RMIcountImpl() throws RemoteException { 16 total = 0; 17 System.out.println( "RMIcountImpl ctor" ); 18 } 19 public void add( int in ) { 20 total += in; 21 System.out.println( "total is " + total ); 22 } 23 public int get() { 24 return total; 25 } } 26 //////////////////////////// RMIcountServer.java \\\\\\\\\\\\\\\\\\\\\\\\\\\ 27 import java.rmi.*; 28 import java.rmi.server.*; 29 public class RMIcountServer { 30 public static void main( String[] args ) { 31 System.setSecurityManager( new RMISecurityManager() ); 32 try { 33 RMIcountImpl obj = new RMIcountImpl(); 34 Naming.rebind( "theOne", obj ); 35 System.out.println( "theOne is bound" ); 36 } catch (Exception ex ) { 37 System.out.println( ex ); 38 } } } 39 //////////////////////////// RMIcountClient.java \\\\\\\\\\\\\\\\\\\\\\\\\\\ 40 import java.rmi.*; 41 import java.rmi.server.*; 42 public class RMIcountClient { 43 public static void main( String[] args ) { 44 System.setSecurityManager( new RMISecurityManager() ); 45 String url = "rmi://trpc04/"; 46 try { 47 RMIcount obj = (RMIcount) Naming.lookup( url + "theOne" ); 48 System.out.print( "enter number (0 for total, -1 to exit): " ); 49 int ans = Read.anInt(); 50 while (ans != -1) { 51 if (ans == 0) 52 System.out.println( " total is " + obj.get() ); 53 else 54 obj.add( ans ); 55 System.out.print( "enter number (0 for total, -1 to exit): " ); 56 ans = Read.anInt(); 57 } 58 } catch (Exception ex ) { 59 System.out.println( ex ); 60 } 61 System.exit( 0 ); 62 } } 63 // -- DO server (RMIcountServer, RMIcountImpl) -- 64 // RMIcountImpl ctor 65 // theOne is bound 66 // total is 1 67 // total is 3 68 // total is 6 69 // total is 10 70 // 71 // -- client application (RMIcountClient) -- 72 // enter number (0 for total, -1 to exit): 1 73 // enter number (0 for total, -1 to exit): 2 74 // enter number (0 for total, -1 to exit): 3 75 // enter number (0 for total, -1 to exit): 0 76 // total is 6 77 // enter number (0 for total, -1 to exit): 4 78 // enter number (0 for total, -1 to exit): 0 79 // total is 10 80 // enter number (0 for total, -1 to exit): -1