我正在尝试学习java中的rmi概念。我成功地在我的机器上运行它并尝试在两台机器上运行但是失败了。 Server.java有
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有
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);
我必须做出哪些改变才能让它适用于两台机器。
请帮助我,提前致谢
答案 0 :(得分:5)
而不是“localhost”在RMI服务器的RMI主机中使用“0.0.0.0”。
<强> EDIT1 强>
以下是另一组更改:
LocateRegistry.createRegistry(port)
Addition
应该是可序列化的(通过UnicastRemoteObject
实现)RemoteException
。box01
/ 1091
)。另请注意,您选择的端口不应已被任何其他服务占用。例如端口1099
。
以下是有效的代码:
<强> 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);
}
}