// RMI fixed. The sort() method is now sorting the copy it receives, and // then returning the copy by value back to the client. The client is now // happy. import java.rmi.*; import java.rmi.server.*; interface RISort extends Remote { int[] sort( int[] array ) throws RemoteException; } public class RmiSortServerFix extends UnicastRemoteObject implements RISort { public RmiSortServerFix() throws RemoteException { } public int[] sort( int[] array ) { System.out.print( "RmiSortServerFix.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 RmiSortServerFix() ); } catch (java.net.MalformedURLException ex) { ex.printStackTrace(); } catch (RemoteException ex) { ex.printStackTrace(); } System.out.println( "SortServerFix 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> rmiregistry // // D:\j2ee\demos> javac RmiSortServerFix.java // D:\j2ee\demos> rmic -v1.2 RmiSortServerFix // D:\j2ee\demos> java RmiSortServerFix // SortServerFix bound in rmiregistry // RmiSortServerFix.sort() - 6 3 2 18 14 2 20 7 12 4 16 20 17 2 8 // RmiSortServerFix.sort() - 12 17 11 18 13 11 18 1 9 14 19 3 2 7 12 // // D:\j2ee\demos> javac RmiSortClientFix.java // D:\j2ee\demos> java RmiSortClientFix // 6 3 2 18 14 2 20 7 12 4 16 20 17 2 8 // 2 2 2 3 4 6 7 8 12 14 16 17 18 20 20 // D:\j2ee\demos> java RmiSortClientFix // 12 17 11 18 13 11 18 1 9 14 19 3 2 7 12 // 1 2 3 7 9 11 11 12 12 13 14 17 18 18 19