package examples.rmi.hello;

import java.io.*;
import java.net.*;
import java.util.*;
import weblogic.rmi.*;
import weblogic.utils.Debug;

/**
 * This client uses the remote HelloServer methods.
 *
 * @author Copyright (c) 1997 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class HelloClient {

  private final static boolean debug = true;
  
  int port;
  String host;
  
  private static void usage() {
    System.err.println("Usage: java examples.rmi.hello.HelloClient " +
                       "<hostname> <port number>");
    System.exit(-1);
  } 
  
  public HelloClient() {}
  
  public static void main(String[] argv) throws Exception {
    if (argv.length < 2) {
      usage();
    }
    String host = argv[0];
    int port = 0;
    try {
      port = Integer.parseInt(argv[1]);
    }
    catch (NumberFormatException nfe) {
      usage();
    }
    try {
      // This uses the more traditional RMI lookup, rather than
      // a JNDI lookup. Looking up via JNDI is preferred, though
      // this method is supported for consistency.
      Hello obj = 
        (Hello)Naming.lookup("rmi://" + host + ':' + port + "/HelloServer");
      System.out.println("Successfully connected to HelloServer on " +
                         host + " at port " +
                         port + ": " + obj.sayHello() );
    }
    catch (Throwable t) {
      t.printStackTrace();
      System.exit(-1);
    }
  }
}
