![](/img/trans.png)
[英]java.rmi.ConnectException: Connection refused to host: 127.0.1.1;
[英]connection refused at 127.0.1.1 java RMI
我是Java RMI技術的新手。 我有一個問題,已經有其他程序員,但我無法理解他們在教程中做了什么,以解決它。 我用Java RMI實現了游戲“tic tac toe”。 這里是ControllerServer代碼
public ControllerServer() {
try {
game = new GameImpl();
BoardView view = new BoardView(this);
viewUpdater = new ServerViewUpdaterImpl(view);
Game gameStub = (Game) UnicastRemoteObject.exportObject(game, 1099);
ServerViewUpdater serverViewStub = (ServerViewUpdater) UnicastRemoteObject.exportObject(viewUpdater, 1099);
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("TTTGame", gameStub);
registry.rebind("TTTServerView", serverViewStub);
} catch (Exception e) {
e.printStackTrace();
}
}
這里是ControllerClient
public ControllerClient() {
try {
BoardView view = new BoardView(this);
localView = new ClientViewUpdaterImpl(view);
String address = JOptionPane.showInputDialog("Insert server's address: ");
Registry registry = LocateRegistry.getRegistry(address, 1099);
game = (Game) registry.lookup("TTTGame");
remoteView = (ServerViewUpdater) registry.lookup("TTTServerView");
remoteView.registerClientView(localView);
} catch (Exception e) {
e.printStackTrace();
}
}
它通過插入“localhost”“127.0.0.1”或我的外部網絡IP在本地工作。 如果客戶端和服務器在不同的計算機上運行,則不起作用。
我得到了“ 127.0.1.1拒絕連接 ”的異常。 我不明白為什么他們試圖在執行的某個時刻使用localhost地址。
正如另一個所說,這是海灘你的IP設置為127.0.1.1
運行ipconfig -a
以查看主機的IP地址。
然后編輯/etc/hosts
文件而不是此行127.0.1.1 "name of the host"
將127.0.1.1替換為您機器的IP 。
這應該工作。
您始終可以通過執行以下命令來驗證rmi服務器正在偵聽的IP:
String hostname = InetAddress.getLocalHost().getHostAddress();
Log.info("this host IP is " + hostname);
如果用正確的IP覆蓋/etc/hosts
文件,那么一切都應該有效。
調用getRegistry()時,地址錯誤。 您需要提供服務器主機的地址。 通常,客戶端主機中不運行RMI注冊表。
這是因為您的IP很可能是錯誤的。 它是127.0.0.1
而不是127.0.1.1
。 您也可以嘗試使用localhost
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.