有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我必须如何配置RMI环境,以便能够在“真正的”网络中使用它?

因为我不想为基于客户机-服务器的应用程序实现通信协议,所以我在两侧实现了一个RMI客户机和一个RMI服务器,用于两个组件之间的信息交换

如果我尝试在同一台机器上启动两个组件来使用我的应用程序,那么一切都正常。但是,如果我将组件拆分到两台不同的计算机上(Kubuntu 9.04作为Windows 7 RC环境中的虚拟机,防火墙被禁用,而Ubuntu 9.04是本机环境),RMI客户端似乎无法执行服务器端定义的方法。(每次函数调用都会导致RMI异常。)

目前,我只在网络接口两侧设置了系统属性“java.rmi.server.hostname”,该网络接口应用于数据交换,并注册了与rmi守护进程通信的默认端口(?)rmid

有人知道可能出了什么问题吗?我是否必须设置一些其他参数,比如“java.rmi.server.codebase”(http://java.sun.com/j2se/1.4.2/docs/guide/rmi/javarmiproperties.html),才能在应用程序中使用rmi功能

编辑:好的,这里有一些额外的信息给你:

在初始化阶段,我的客户端尝试与服务器组件的RMI服务器建立连接,该组件是使用以下两种方法初始化的:

private void initialize()
{
    // set ip address of rmi server
    System.setProperty("java.rmi.server.hostname", ipAddress);

    // try to register rmi server
    try
    {
        LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
    }
    catch (Exception e)
    {
        // ignore
    }
}

public void start()
{
    System.out.print("starting master control RMI server ...");

    try
    {
        Naming.rebind("MasterControl", this);
    }
    catch (Exception e)
    {
        System.out.println("error: could not initialize master control RMI server");
        System.exit(1);
    }

    // set running flag
    isRunning = true;

    System.out.println(" done");
}

“ipAddress”是服务器组件网络接口的ip地址

客户端组件用于建立连接的方法如下所示:

    public void connect()
{
    // build connection url
    String url = "rmi://" + masterControlIpAddress + "/MasterControl";

    System.out.println(url);

    System.out.print("connecting to master control ...");

    // try to connect to master control server
    while (connection == null)
    {
        try
        {
            connection = (MasterControlInterface) Naming.lookup(url);
            id = connection.register(localIpAddress);
        }
        catch (Exception e)
        {
            // ignore
        }

        if (connection == null)
        {
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }

    System.out.println(" done");
}

正如您所见,我的客户端调用一个函数来在服务器上注册连接:

@Override
public int register(String ipAddress) throws RemoteException
{
    // add connection to registrationHandler
    masterControl.registrationHandler.addConnection(ipAddress);

    // log
    int connectionCount = masterControl.registrationHandler.getConnectionCount();
    System.out.println("slave control (" + ipAddress + ") instance has been registered at the master control server under the following id: " + connectionCount);

    return connectionCount;
}

如果我使用真实的网络连接运行我的程序,文本“slave control…”不显示在服务器端。因此,我不确定该函数是否真的由客户端组件调用

客户机组件初始化后,它会尝试通过使用与服务器的RMI连接调用以下方法来通知服务器组件:

public void sendInitializationDone()
{
    try
    {
        connection.initializationDone();
    }
    catch (RemoteException e)
    {
        System.out.println("error: could not send 'initializationDone' message to master control");
        System.out.println(e);
        System.exit(1);
    }
}

在服务器端设置标志

错误发生在客户端的此函数内部:

爪哇。rmi。ConnectException:连接拒绝承载127.0.1.1;嵌套的例外是:java。网ConnectException:连接被拒绝

我不知道主持人为什么在这里127.0.1.1

@nos

当然,我禁用了windows防火墙和卡巴斯基互联网安全的保护机制。我不认为我的Kubuntu有运行的防火墙。一般来说,建立连接是可能的,因为我已经使用scp将我的程序复制到了另一台机器上

编辑2:

Mhhh,在/etc/hosts中设置了将机器指向机器ip地址的条目后,它似乎可以工作,但并不真正理解为什么会工作

比尔

马库斯


共 (0) 个答案