1 16 17 package examples; 18 19 import java.rmi.Remote ; 20 import java.rmi.server.UnicastRemoteObject ; 21 import java.rmi.RemoteException ; 22 import java.rmi.Naming ; 23 import java.util.Vector ; 24 import java.io.*; 25 26 48 public class NumberCruncherClient { 49 50 51 public static void main(String [] args) { 52 if(args.length == 1) { 53 try { 54 String url = "rmi://"+args[0]+ "/Factor"; 55 NumberCruncher nc = (NumberCruncher) Naming.lookup(url); 56 loop(nc); 57 } 58 catch(Exception e) { 59 e.printStackTrace(); 60 } 61 } 62 else 63 usage("Wrong number of arguments."); 64 } 65 66 static 67 void usage(String msg) { 68 System.err.println(msg); 69 System.err.println( 70 "Usage: java org.apache.log4j.examples.NumberCruncherClient HOST\n" + 71 " where HOST is the machine where the NumberCruncherServer is running."); 72 System.exit(1); 73 } 74 75 76 static 77 void loop(NumberCruncher nc) { 78 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 79 int i = 0; 80 while (true) { 81 System.out.print("Enter a number to factor, '-1' to quit: "); 82 try { 83 i = Integer.parseInt(in.readLine()); 84 } 85 catch(Exception e) { 86 e.printStackTrace(); 87 } 88 if(i == -1) { 89 System.out.print("Exiting loop."); 90 return; 91 } 92 else { 93 try { 94 System.out.println("Will attempt to factor "+i); 95 int[] factors = nc.factor(i); 96 System.out.print("The factors of "+i+" are"); 97 for(int k=0; k < factors.length; k++) { 98 System.out.print(" " + factors[k]); 99 } 100 System.out.println("."); 101 } 102 catch(RemoteException e) { 103 System.err.println("Could not factor "+i); 104 e.printStackTrace(); 105 } 106 } 107 } 108 } 109 } 110 | Popular Tags |