// The Sort class has migrated from Remote to Serializable. When the // server calls rebind(), a copy of RmiSortServerValue is deposited in // the rmiregistry, and the server main() is done. The client then // executes in the same Command Prompt window. When lookup() is called, // a copy of the serializable RmiSortServerValue object materializes in // the client's main(), and runs locally. import java.rmi.*; import java.rmi.server.*; // without extends java.rmi.Remote - // ERROR: java.rmi.Naming.rebind(String,Remote) cannot be applied to // (String,RmiSortServerValue) interface RISort extends java.io.Serializable, java.rmi.Remote { int[] sort( int[] array ); } public class RmiSortServerValue implements RISort { public int[] sort( int[] array ) { System.out.print( "RmiSortServerValue.sort() - " ); for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); for (int g = array.length/2; g > 0; g /= 2) for (int i = g; i < array.length; i++) for (int j = i-g; j >= 0; j -= g) if (array[j] > array[j+g]) { int temp = array[j]; array[j] = array[j+g]; array[j+g] = temp; } return array; } public static void main( String[] args ) { try { Naming.rebind( "SortServer", new RmiSortServerValue() ); } catch (java.net.MalformedURLException ex) { ex.printStackTrace(); } catch (RemoteException ex) { ex.printStackTrace(); } System.out.println( "RmiSortServerValue bound in rmiregistry" ); } } import java.rmi.*; public class RmiSortClientFix { public static void main( String[] args ) { int[] array = new int[15]; java.util.Random rn = new java.util.Random(); for (int i=0; i < array.length; i++) array[i] = rn.nextInt(20) + 1; for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); try { RISort sorter = (RISort) Naming.lookup( "SortServer" ); array = sorter.sort( array ); } catch (NotBoundException ex) { ex.printStackTrace(); } catch (java.net.MalformedURLException ex) { ex.printStackTrace(); } catch (RemoteException ex) { ex.printStackTrace(); } for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); } } // D:\j2ee\demos> start rmiregistry // D:\j2ee\demos> javac RmiSortServerValue.java // D:\j2ee\demos> java RmiSortServerValue // RmiSortServerValue bound in rmiregistry // D:\j2ee\demos> javac RmiSortClientFix.java // D:\j2ee\demos> java RmiSortClientFix // 11 3 8 19 5 17 20 17 3 9 12 19 14 1 2 // RmiSortServerValue.sort() - 11 3 8 19 5 17 20 17 3 9 12 19 14 1 2 // 1 2 3 3 5 8 9 11 12 14 17 17 19 19 20 // D:\j2ee\demos> java RmiSortClientFix // 17 13 18 13 16 1 6 19 12 17 1 10 18 7 6 // RmiSortServerValue.sort() - 17 13 18 13 16 1 6 19 12 17 1 10 18 7 6 // 1 1 6 6 7 10 12 13 13 16 17 17 18 18 19