我试图在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);
我必须做出哪些改变才能让它适用于两台机器.
请帮助我,提前致谢
解决方法:
而不是“localhost”在RMI服务器的RMI主机中使用“0.0.0.0”.
EDIT1
以下是另一组更改:
>在您希望服务器侦听LocateRegistry.createRegistry(端口)的端口上创建一个注册表
>您的RMI类添加应该是Serializable(通过UnicastRemoteObject实现)
> RMI方法应在其签名中抛出RemoteException.
> RMI客户端应该知道远程计算机的名称/端口(在本例中为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);
}
}