gpt4 book ai didi

java - RMI - 在同一台机器上工作,而不是在 LAN 中

转载 作者:行者123 更新时间:2023-12-01 18:51:27 25 4
gpt4 key购买 nike

我正在尝试在一台服务器和两个客户端之间建立两种通信。当所有程序在同一台计算机上运行时,这非常有效,但当我尝试使用 LAN 网络时,它不起作用。我收到错误:

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

这是服务器代码:

public class Server{
private Game partie; // The class Game extends UnicastRemoteObject and implements ServerInterface


public Server() throws RemoteException {
System.setProperty("java.rmi.server.hostname", "192.168.1.24");
partie = new Game();
LocateRegistry.createRegistry(1099);
try{
Naming.rebind("Server", partie);
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String argv[]) throws RemoteException{
new Server();
}
}

这是客户端代码的构造函数:

public Client(String aName, String aServerAdress) throws RemoteException {
super();
name = aName;
ServerAdress = aServerAdress; // = "192.168.1.24"
theRegistry = LocateRegistry.getRegistry(ServerAdress);

try {
serverInterface = (ServerInterface) theRegistry.lookup("Server");
} catch (NotBoundException e1) {
e1.printStackTrace();
}



try {
theRegistry.bind(name, this); // For two-way communication
} catch (AlreadyBoundException e) {
e.printStackTrace();
}


serverInterface.registerClient(name);
}

registerClient(String name) 代码大约在哪里(在 Game 类中):

cd_client = (ClientInterface) Naming.lookup("rmi://127.0.0.1/" + name);

所有防火墙均已禁用。

我已经研究这个问题很多小时了,但仍然没有发现问题所在。如果您能帮我一点忙,我将不胜感激。谢谢

最佳答案

将所有出现的 127.0.0.1(注册表绑定(bind)除外)更改为您的 LAN IP 地址(在您的情况下为 192.168.1.24)

127.0.0.1 是 Loopback地址:

"Loopback (loop-back) describes ways of routing electronic signals, digital data streams, or flows of items from their originating facility back to the receiving end of the source without intentional processing or modification. This is primarily a means of testing the transmission or transportation infrastructure." -- from Wikipedia

关于java - RMI - 在同一台机器上工作,而不是在 LAN 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15818699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com