【问题标题】:Example of a rmi program involving 2 machine connected via LAN涉及通过 LAN 连接的 2 台机器的 rmi 程序示例
【发布时间】:2014-02-01 07:27:34
【问题描述】:

我正在尝试在 java 中学习 rmi 的概念。我成功地在我的机器上运行它并尝试在两台机器上运行它但失败了。 Server.java 有

System.setSecurityManager(new RMISecurityManager());
        Addition Hello = new Addition();    
        Registry registry = LocateRegistry.createRegistry(5432);
        //Addition stub = (Addition) UnicastRemoteObject.exportObject(Hello,6789);
        registry.rebind("lookupthis", Hello);
        System.out.println("Addition Server is ready.");

Client.java 有

Registry reg=LocateRegistry.getRegistry("[ip of server]",5432);
            hello = (AdditionalInterface)Naming.lookup("lookupthis");
            int result=hello.Add(9,10);
            System.out.println("Result is :"+result);

我必须进行哪些更改才能使其适用于两台机器。

请帮助我,在此先感谢

【问题讨论】:

  • 服务器代码是否仍然显式绑定到它的环回地址?
  • 是的,我应该将 localhost 更改为 [ip of client] 吗?
  • 看看下面的答案。

标签: java rmi


【解决方案1】:

在 RMI 服务器的 RMI 主机中使用“0.0.0.0”代替“localhost”。

EDIT1

以下是另一组更改:

  • 在您希望服务器监听的端口上创建一个注册表LocateRegistry.createRegistry(port)
  • 您的 RMI 类 Addition 应该是可序列化的(通过 UnicastRemoteObject 实现)
  • RMI 方法应在其签名中添加RemoteException
  • RMI 客户端应该知道远程机器的名称/端口(在本例中为 box01 / 1091)。

另外请注意,您选择的端口不应已被任何其他服务占用。比如端口1099

下面是有效的代码:

AdditionalInterface.java

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface AdditionalInterface extends Remote {
    public int Add(int a, int b) throws RemoteException;
}

Addition.java

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Addition extends UnicastRemoteObject implements
        AdditionalInterface {
    private static final long serialVersionUID = 1L;

    public Addition() throws RemoteException {
        // TODO Auto-generated constructor stub
    }

    public int Add(int a, int b) {
        return a + b;
    }
}

AdditionServer.java

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class AdditionServer {
    public static void main(String[] argv) throws RemoteException {
        Addition Hello = new Addition();

        int port = 1091;

        try { // special exception handler for registry creation
            LocateRegistry.createRegistry(port);
            System.out.println("java RMI registry created.");
        } catch (RemoteException e) {
            // do nothing, error means registry already exists
            System.out.println("java RMI registry already exists.");
        }

        String hostname = "0.0.0.0";

        String bindLocation = "//" + hostname + ":" + port + "/Hello";
        try {
            Naming.bind(bindLocation, Hello);
            System.out.println("Addition Server is ready at:" + bindLocation);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("Addition Serverfailed: " + e);
        }
    }
}

AdditionClient

import java.net.MalformedURLException;
import java.rmi.*;

public class AdditionClient {
    public static void main(String[] args) {
        String remoteHostName = "box01";
        int remotePort = 1091;
        String connectLocation = "//" + remoteHostName + ":" + remotePort
                + "/Hello";

        AdditionalInterface hello = null;
        try {
            System.out.println("Connecting to client at : " + connectLocation);
            hello = (AdditionalInterface) Naming.lookup(connectLocation);
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (NotBoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        int result = 0;
        try {
            result = hello.Add(9, 10);
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("Result is :" + result);

    }
}

【讨论】:

  • 添加服务器失败:java.lang.NullPointerException
  • 0.0.0.0 不是有效的目标 IP 地址。绑定 URL 应使用“localhost”,查找 URL 应使用服务器主机名。远程对象不需要可序列化。客户端连接到服务器,而不是其他客户端。 -1
  • @EJP: 0.0.0.0 是一个有效的 IP 地址,这意味着 default route address。阅读:stackoverflow.com/questions/3655723/…
  • 您引用的链接引用 RFC 1700 说 0.0.0.0 仅作为源地址有效;没有任何内容表明它是解析为本地主机的有效目标地址;并且问题中没有任何内容需要默认路由地址。
  • @EJP 我所知道的每个 IP 堆栈都使用全零来表示“侦听所有接口上的连接”。使用 localhost 意味着服务器将只接受来自自身的连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多