Java Reference
In-Depth Information
try {
// Create registry on local machine running on port 1099
LocateRegistry.createRegistry(1099);
System.out.println("RMI registry was created");
} catch (RemoteException e) {
System.out.println("RemoteException occurred: ");
e.printStackTrace();
}
RMIServer theServer = new RMIServer();
// Bind the server instance to the name "RMIServer"
Naming.rebind("//localhost/RMIServer", theServer);
System.out.println("RMIServer bound in registry");
}
}
4. RMIClient just contains a main method to try out the RMI server:
package rmiexample;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class RMIClient {
public static void main(String args[]) throws Exception {
// Get RMI registry running at the local computer
Registry registry = LocateRegistry.getRegistry("localhost");
// Request interface bound on name "RMIServer"
RMIInterface serverInterface = (RMIInterface) registry.lookup("RMIServer");
// Execute methods over RMI
System.out.println(serverInterface.addTwoNumbers(3, 4));
System.out.println(serverInterface.substractTwoNumbers(10, 3));
}
}
5.
To try out this code, start the main class of RMIServer first, followed by RMIClient . If everything
goes right, you should see this on the server's console:
Starting server...
RMI registry was created
RMIServer bound in registry
addTwoNumbers was called
substractTwoNumbers was called
With the results of the two method calls on the client's console:
7
7
Search WWH ::




Custom Search