我是 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 拒绝”。我不明白他们为什么在执行的某个时候尝试使用本地主机地址。