0

我正在尝试在一台服务器和两个客户端之间建立双向通信。当所有程序都在同一台机器上运行时,这非常有效,但当我尝试使用 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);

所有防火墙都被禁用。

我已经在这个问题上工作了好几个小时,但我仍然没有发现问题所在。如果您能帮我一点忙,我将不胜感激。谢谢

4

1 回答 1

4

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

127.0.0.1 是一个环回地址:

“环回 (loop-back) 描述了将电子信号、数字数据流或项目流从其原始设施路由回源的接收端而无需有意处理或修改的方法。这主要是一种测试传输或交通基础设施。” ——来自维基百科

于 2013-04-04T18:24:06.827 回答