FileDocCategorySizeDatePackage
FibonacciClient2.javaAPI DocExample1420Sat Sep 09 21:21:36 BST 2000None

FibonacciClient2.java

// You'll need to change the name of this file to
// FibonacciClient to get it to compile. 

import java.rmi.*;
import java.net.*;
import java.math.BigInteger;


public class FibonacciClient {

  public static void main(String args[]) {
        
    if (args.length == 0 || !args[0].startsWith("rmi://")) {
      System.err.println(
"Usage: java Fibonacci client rmi://host.domain:port/fibonacci number");
      return;   
    }
            
    if (System.getSecurityManager() == null) {
      System.setSecurityManager(new LaxSecurityManager());
    }          

    try {      
      Object o = Naming.lookup(args[0]);
      Fibonacci calculator = (Fibonacci) o;
      for (int i = 1; i < args.length; i++) {
        try {
          BigInteger index = new BigInteger(args[i]);
          BigInteger f = calculator.getFibonacci(index);
          System.out.println("The " + args[i] + "th Fibonacci number is " 
           + f);
        }
        catch (NumberFormatException e) {
          System.err.println(args[i] + "is not an integer.");
        }
      } 
    }
    catch (MalformedURLException e) {
      System.err.println(args[0] + " is not a valid RMI URL");
    }
    catch (RemoteException e) {
      System.err.println("Remote object threw exception " + e);
    }
    catch (NotBoundException e) {
      System.err.println(
       "Could not find the requested remote object on the server");
    } 

  }

}