42 lines
1.3 KiB
Java
42 lines
1.3 KiB
Java
package client;
|
|
|
|
// Client/Client.java class represents the client that has to relay on the Hello engine
|
|
// to perform tasks remotely.
|
|
// it needs the Hello interface from Hello package
|
|
import hello.Hello;
|
|
|
|
// it also needs those to access the registry,
|
|
// so it can get remote object reference of the server
|
|
import java.rmi.registry.LocateRegistry;
|
|
import java.rmi.registry.Registry;
|
|
import java.util.Scanner;
|
|
|
|
public class Client {
|
|
private Client() {
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
// normally, the address of the server is passed here on the command line as an
|
|
// argument
|
|
// we get that address from args[0] if provided, else we use null for localhost
|
|
String host = (args.length < 1) ? null : args[0];
|
|
String name = "Hello";
|
|
|
|
try (Scanner scanner = new Scanner(System.in);) {
|
|
|
|
// get the registry from host(server)
|
|
Registry registry = LocateRegistry.getRegistry(host);
|
|
|
|
// look up the remote object by name in the registry
|
|
// stub is the a reference to the remote object
|
|
Hello stub = (Hello) registry.lookup(name);
|
|
String response = stub.sayHello();
|
|
System.out.println("response: " + response);
|
|
|
|
} catch (Exception e) {
|
|
System.err.println("Client exception: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|