我是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();
}
}
Run Code Online (Sandbox Code Playgroud)
这里是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();
}
}
Run Code Online (Sandbox Code Playgroud)
它通过插入"localhost""127.0.0.1"或我的外部网络IP在本地工作.如果客户端和服务器在不同的计算机上运行,则不起作用.
我得到了" 127.0.1.1拒绝连接 "的异常.我不明白为什么他们试图在执行的某个时刻使用localhost地址.
正如对方所说,这是海滩,您的 IP 设置为127.0.1.1
运行 aipconfig -a查看您的主机的 IP 地址是什么。
然后编辑该文件,将 127.0.1.1
替换为您计算机的 IP,/etc/hosts而不是这一行
。127.0.1.1 "name of the host"
这应该有效。
您始终可以通过执行以下命令来验证rmi 服务器正在侦听的IP :
String hostname = InetAddress.getLocalHost().getHostAddress();
Log.info("this host IP is " + hostname);
Run Code Online (Sandbox Code Playgroud)
如果您/etc/hosts使用正确的 IP 覆盖该文件,那么一切都会正常。