Contents
- Client.java
- Hello.java
- Server.java
Client.java 1/3
[top][prev][next]
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* RMI Client
*
* @author Sara Sprenkle
*/
public class Client {
public static void main(String[] args) {
String host = (args.length < 1) ? "localhost" : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host, 8888);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
int[] results = stub.sumDifference(30, 40);
System.out.println("sumDifference response: " + results[0] + "\n" + results[1]);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
}
}
}
Hello.java 2/3
[top][prev][next]
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
* Interface for the server's available methods
*
* @author Sara Sprenkle
*/
public interface Hello extends Remote {
String sayHello() throws RemoteException;
int[] sumDifference(int x, int y) throws RemoteException;
}
Server.java 3/3
[top][prev][next]
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
/**
* RMI Server
*
* @author Sara Sprenkle
*/
public class Server implements Hello {
public Server() {
}
/**
* @return "Hello, world!"
*/
public String sayHello() {
return "Hello, world!";
}
/**
* @return the sum and difference of x and y
*/
public int[] sumDifference(int x, int y) {
int[] results = new int[2];
results[0] = x + y;
results[1] = y - x;
return results;
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
Registry registry = LocateRegistry.createRegistry(8888);
registry.bind("Hello", stub);
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
}
}
}
Generated by GNU Enscript 1.6.6.