started task 7
This commit is contained in:
41
week3_TinsaeGhilay/Task 3/src/client/Client.java
Normal file
41
week3_TinsaeGhilay/Task 3/src/client/Client.java
Normal file
@@ -0,0 +1,41 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user