/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. /////////  IntPhase.java   ///////////////// 

2.  

3. public interface IntPhase 

4.     extends java.rmi.Remote 

5.  

6.

7.   public int increment ( int p ) 

8.       throws java.rmi.RemoteException

9.  

10.   public Integer iincrement ( int p ) 

11.       throws java.rmi.RemoteException

12.

13.  

14. ////////////  RMIServer.java   /////////////////// 

15.  

16. import java.rmi.*; 

17. import java.rmi.server.UnicastRemoteObject

18.  

19. public class RMIServer 

20.     extends UnicastRemoteObject 

21.     implements IntPhase 

22.  

23.

24.   private int sum ; 

25.    

26.   public 

27.   RMIServer() throws RemoteException 

28.   { 

29.     super(); 

30.     sum = 0

31.   } 

32.  

33.   public int 

34.   increment ( int plus ) throws RemoteException 

35.   { 

36.     sum += plus ; 

37.     return sum ; 

38.   } 

39.   public Integer 

40.   iincrement ( int plus ) throws RemoteException 

41.   { 

42.     sum += plus ; 

43.     return new Integer (sum) ; 

44.   } 

45.  

46.   static public void 

47.   main(String[] args) { 

48.  

49.     System.setSecurityManager ( new RMISecurityManager () ) ; 

50.     try

51.       IntPhase  serv = new RMIServer(); 

52.  

53.       Naming.rebind 

54.       ( "rmi://localhost:1099/TheServant", serv ) ; 

55.  

56.       System.out.println ( "=== SERVER READY ===" ); 

57.     } 

58.     catch ( Exception e ) 

59.     { 

60.       System.out.println ( "Error : " + e ); 

61.       e.printStackTrace(); 

62.     } 

63.   } 

64.

65. ////////////  RMIClient.java   /////////////////// 

66.  

67. import java.rmi.*; 

68.  

69. public class RMIClient

70.  

71.   static public void 

72.   main (String[] args) 

73.   { 

74.     System.setSecurityManager ( new RMISecurityManager () ) ; 

75.     try

76.       IntPhase theserv = ( IntPhase ) Naming.lookup 

77.         ( "rmi://localhost/TheServant" ) ; 

78.  

79.       int cnt = theserv.increment ( 5 ); 

80.  

81.       System.out.println ( cnt ); 

82.  

83.       Integer icnt = theserv.iincrement ( 4 ); 

84.  

85.       System.out.println ( icnt ); 

86.     } 

87.     catch ( Exception e ) 

88.     { System.out.println ( "Error : " + e ); } 

89.  

90.     try

91.       String[] lst = Naming.list("rmi://localhost"); 

92.  

93.       for ( int i=0; i < lst.length; i++ ) 

94.         System.out.println ( lst[i] ); 

95.     } 

96.     catch (Exception e) 

97.     { System.out.println ( e ); } 

98.  

99.     System.out.println ( "=== CLIENT FINISHED ===" ); 

100.   } 

101.  

102.   public RMIClient() 

103.   {  } 

104.

105. //==========  in console window  ============= 

106. set CLASSPATH=  or unset CLASSPATH 

107. start  rmiregistry  -J-verbose 

108.  

109.  

110. (.....go to classes + r.pol directory ) 

111. java 

112.  -Djava.rmi.server.codebase=file://localhost/e:/netfiles/ 

113.  -Djava.security.policy=r.pol 

114.  RMIServer 

115.  

116. //==== another console window, different directory  === 

117. set CLASSPATH

118.  

119. java -Djava.security.policy=r.pol  RMIClient 

120.  

121. //==========  r.pol   =========== 

122. grant { 

123. permission java.net.SocketPermission "*:1024-65535", "connect,accept"

124. permission java.io.FilePermission "e:\\netfiles\\-", "read"

125. }; 

126. //================================= 

127.  -Djava.rmi.server.codebase=http://localhost/netfiles/ 

128. //==========  rh.pol   =========== 

129. grant { 

130. permission java.net.SocketPermission "*:1024-65535", "connect,accept"

131. permission java.net.SocketPermission "*:80", "connect"

132. }; 

133. //================================ 

134. \jdk1.3\bin\rmic.exe  RMIServer 

135.