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

标签 java networking rmi lan rmiregistry

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

相关文章:

java - 如何在 Spring JDBCTemplate 中正确使用准备好的语句?

java - 无法在硬编码 java 应用程序中的 jFrame 之间进行更改?

android - 如何获取 "network"时间(来自称为 "Automatic"的 "Use network-provided values"设置),而不是手机上的时间?

Java RMI : exception: java. security.AccessControlException:访问被拒绝 ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")

rmi - 当cleint调用registry.lookup ("RMI Server"时,获取RMI Server中的客户端IP地址

java - 未知异常原因

java - 在Java中执行while循环

c - 在 Linux 上获取有关网络接口(interface)更改的通知

c++ - SIGPIPE,断管

java - RMI 解码异常