繁体   English   中英

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系统属性java.rmi,server.hostname为127.0.0.1。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM