RMI 服务器使用了错误的 IP 地址
RMI Server uses wrong IP address
当我的网卡连接到网络 A 时,我正在启动我的 RMI 服务器。运行 我的客户端按预期成功并打印 "Hello World"。一旦我将网络连接更改为网络 B(无需重新启动我的 RMI 服务器!),我就无法再连接到服务器。
服务器和客户端始终 运行 在同一台主机上,因此使用本地主机地址就足够了。
服务器:
public class HelloImpl extends UnicastRemoteObject implements Hello {
public HelloImpl() throws RemoteException {
}
public String sayHello() {
return "Hello world!";
}
public static void main(String args[]) throws RemoteException {
Registry registry = LocateRegistry.createRegistry(3128);
registry.rebind("HelloServer", new HelloImpl());
}
}
客户:
public class HelloClient {
public static void main(String arg[]) throws Exception{
Registry registry=LocateRegistry.getRegistry("localhost", 3128);;
Hello result = (Hello) registry.lookup("HelloServer");
System.out.println(result.sayHello());
}
}
例外情况是:
HelloClient exception: Connection refused to host: 192.168.169.136; nested exception is:
java.net.ConnectException: Connection timed out: connect
这是指我在连接到网络 A 时分配的 IP 地址。注册表查找按预期工作,只有对 result.sayHello()
的调用失败,出现上述异常。
如何告诉 RMI 对所有内容(而不仅仅是注册表)使用 localhost?
在导出任何远程对象(包括注册表)之前,在服务器 JVM 上将系统 属性 java.rmi,server.hostname
设置为 127.0.0.1。
当我的网卡连接到网络 A 时,我正在启动我的 RMI 服务器。运行 我的客户端按预期成功并打印 "Hello World"。一旦我将网络连接更改为网络 B(无需重新启动我的 RMI 服务器!),我就无法再连接到服务器。 服务器和客户端始终 运行 在同一台主机上,因此使用本地主机地址就足够了。
服务器:
public class HelloImpl extends UnicastRemoteObject implements Hello {
public HelloImpl() throws RemoteException {
}
public String sayHello() {
return "Hello world!";
}
public static void main(String args[]) throws RemoteException {
Registry registry = LocateRegistry.createRegistry(3128);
registry.rebind("HelloServer", new HelloImpl());
}
}
客户:
public class HelloClient {
public static void main(String arg[]) throws Exception{
Registry registry=LocateRegistry.getRegistry("localhost", 3128);;
Hello result = (Hello) registry.lookup("HelloServer");
System.out.println(result.sayHello());
}
}
例外情况是:
HelloClient exception: Connection refused to host: 192.168.169.136; nested exception is:
java.net.ConnectException: Connection timed out: connect
这是指我在连接到网络 A 时分配的 IP 地址。注册表查找按预期工作,只有对 result.sayHello()
的调用失败,出现上述异常。
如何告诉 RMI 对所有内容(而不仅仅是注册表)使用 localhost?
在导出任何远程对象(包括注册表)之前,在服务器 JVM 上将系统 属性 java.rmi,server.hostname
设置为 127.0.0.1。