I am trying to learn concepts of rmi in java. I succesfully run it in my machine and tried out to do it on two machine but failed. Server.java has
System.setSecurityManager(new RMISecurityManager());
Addition Hello = new Addition();
Registry registry = LocateRegistry.createRegistry(5432);
//Addition stub = (Addition) UnicastRemoteObject.exportObject(Hello,6789);
registry.rebind("lookupthis", Hello);
System.out.println("Addition Server is ready.");
Client.java has
Registry reg=LocateRegistry.getRegistry("[ip of server]",5432);
hello = (AdditionalInterface)Naming.lookup("lookupthis");
int result=hello.Add(9,10);
System.out.println("Result is :"+result);
What changes i have to make it work for two machines.
Please help me with this, Thanks in Advance
Instead of "localhost" use "0.0.0.0" in RMI host of the RMI Server.
EDIT1
Here are another set of changes:
LocateRegistry.createRegistry(port)
Addition
should be Serializable ( achieved via UnicastRemoteObject
)RemoteException
in their signature.box01
/ 1091
).Also note that the port you chose should not be already occupied by any other service. Such as port 1099
.
Below is the code that works:
AdditionalInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface AdditionalInterface extends Remote {
public int Add(int a, int b) throws RemoteException;
}
Addition.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Addition extends UnicastRemoteObject implements
AdditionalInterface {
private static final long serialVersionUID = 1L;
public Addition() throws RemoteException {
// TODO Auto-generated constructor stub
}
public int Add(int a, int b) {
return a + b;
}
}
AdditionServer.java
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class AdditionServer {
public static void main(String[] argv) throws RemoteException {
Addition Hello = new Addition();
int port = 1091;
try { // special exception handler for registry creation
LocateRegistry.createRegistry(port);
System.out.println("java RMI registry created.");
} catch (RemoteException e) {
// do nothing, error means registry already exists
System.out.println("java RMI registry already exists.");
}
String hostname = "0.0.0.0";
String bindLocation = "//" + hostname + ":" + port + "/Hello";
try {
Naming.bind(bindLocation, Hello);
System.out.println("Addition Server is ready at:" + bindLocation);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
System.out.println("Addition Serverfailed: " + e);
}
}
}
AdditionClient
import java.net.MalformedURLException;
import java.rmi.*;
public class AdditionClient {
public static void main(String[] args) {
String remoteHostName = "box01";
int remotePort = 1091;
String connectLocation = "//" + remoteHostName + ":" + remotePort
+ "/Hello";
AdditionalInterface hello = null;
try {
System.out.println("Connecting to client at : " + connectLocation);
hello = (AdditionalInterface) Naming.lookup(connectLocation);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NotBoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int result = 0;
try {
result = hello.Add(9, 10);
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Result is :" + result);
}
}