簡體   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