简体   繁体   English

RMI服务器使用错误的IP地址

[英]RMI Server uses wrong IP address

I am starting my RMI server while my network card is connected to Network A. Running my client succeeds as expected and prints "Hello World". 当我的网卡连接到网络A时,我正在启动RMI服务器。运行客户端成功,并显示“ Hello World”。 As soon as I change my Network connection to Network B (without restarting my RMI server!) I am not able to connect to the server anymore. 一旦将我的网络连接更改为网络B(无需重新启动RMI服务器!),我将无法再连接到该服务器。 Server and client always run on the same host, so using localhost address would be sufficient. 服务器和客户端始终在同一主机上运行,​​因此使用本地主机地址就足够了。

Server: 服务器:

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());
    }
}

Client: 客户:

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());
    }

}

Exception is: 例外是:

HelloClient exception: Connection refused to host: 192.168.169.136; nested exception is: 
    java.net.ConnectException: Connection timed out: connect

Which refers to my IP address that was assigned while being connected to Network A. There Registry lookup works as expected, only the call to result.sayHello() fails with the exception above. 这是指我连接到网络A时分配的IP地址。注册表查找按预期方式工作,只有对result.sayHello()的调用失败,但以上情况除外。

How to tell RMI to use localhost for everything (and not only the registry)? 如何告诉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