用于远程ip(主机)的Java RMI [英] Java RMI for remote ip (host)

查看:146
本文介绍了用于远程ip(主机)的Java RMI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手。我无法正确理解 RMI 。互联网上有大量的教程,但我所能理解的都是本地主机。服务器和客户端都在同一台机器上运行。

I am newbie. I cannot understand RMI correctly. There are tons of tutorials available on the internet ,but all of them are for the local host as I can understand. Both server and client run on the same machine.

我想在任何机器上运行客户端,主机将在一台计算机上让我们考虑 IP - 11.11.11.11 。在 1099

但是我怎样才能实现这个目标,我应该在哪里指定客户端的IP。据我所知,使用命名转换,如DNS,但无论如何,当我需要远程连接到某台机器时,我至少需要知道IP地址(+掩码)和端口。

I want to run client on any machine and the host will be on the one computer lets consider IP - 11.11.11.11. On the 1099.
But how can I achieve this, where should I specify my IP on the client side. As I understand naming convertion is used, like DNS but anyway when I need to connect to some machine remotely I need to know at least IP address (+mask) and port.

我想我错过了一些非常重要的事情。

I guess I missed something really important.

请举例说明如何在同一主机上远程配置RMI。

Please give some example how to configure RMI remotly not on the same host.

推荐答案

首先你必须设置一个服务器,其方法或对象可以被任何远程客户端访问
下面是服务器的示例代码。

First you have to setup a server whose method or object can be accessed by any remote client Below is example code for the server.

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

public interface MyCalc extends Remote{
    int add(int a, int b) throws RemoteException;
}




import java.rmi.RemoteException;

public class MyCalcImpl implements MyCalc {

    @Override
    public int add(int a, int b) throws RemoteException {
        return (a + b);
    }
}

启动服务器机器上的rmi注册表,以便注册你的对象是这个注册表,你最好在你放置类的地方运行它,否则你会获得ClassNotFound。

Start the rmi registry on server machine so you can register your object to this registry and better you run it where you have placed your classes otherwise you will get ClassNotFound.

    rmiregistry 1099  

注意:如果端口已在使用中,您可能需要更改端口。

Note: you might need to change the port if port is already in use.

将您的对象注册到名为'calculator'的rmi注册表。

Register you object to rmi registry with name 'calculator'.

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class MyRMIServer {
    public static void main(String[] args) throws Exception {

        System.setProperty("java.security.policy","file:///tmp/test.policy");

        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Calculator";
            MyCalc engine = new MyCalcImpl();
            MyCalc stub = (MyCalc) UnicastRemoteObject.exportObject(engine, 0);
            Registry registry = LocateRegistry.getRegistry(1099);
            System.out.println("Registering Calculator Object");
            registry.rebind(name, stub);
        } catch (Exception e) {
            System.err.println("Exception:" + e);
            e.printStackTrace();
        }
    }
}

注意:运行程序你必须设置一个安全策略文件,并为该创建一个文件,例如test.policy并复制到内容之下。

Note: To run the program you have to setup a security policy file and for that creat a file e.g. test.policy and copy below content.

grant {
    permission java.security.AllPermission;
    permission java.net.SocketPermission "localhost:1099", "connect, resolve";
    permission java.net.SocketPermission "127.0.0.1:1099", "connect, resolve";
    permission java.net.SocketPermission "localhost:80", "connect, resolve";
};

您可以根据具体情况更改IP和端口。

You can change IP and port as per your case.

启动服务器后,假设您的服务器的IP地址是11.11.11.11,那么您可以在服务器上调用MyCalc的add()。因此,在您的客户端计算机上,您的客户端代码将类似于:

After starting the server, suppose your server's IP address is 11.11.11.11 then you can invoke the MyCalc's add() on the server. So on your client machine your client code would be like:

将MyCalc类从服务器复制到客户端计算机,以便在编译客户端代码时将其设置为类路径。 / p>

Copy the MyCalc class from server to client machine so you can set it to the classpath while compiling client's code.

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class MyRMIClient {
    public static void main(String args[]) {

        System.setProperty("java.security.policy","file:///tmp/test.policy");

        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }
        try {
            String name = "Calculator";
            String serverIP = "11.11.11.11"; // or localhost if client and server on same machine.
            int serverPort = 1099;
            Registry registry = LocateRegistry.getRegistry(serverIP, serverPort);
            MyCalc mycalc = (MyCalc) registry.lookup(name);
            int result = mycalc.add(10, 20);
            System.out.println("Result:" + result);
        } catch (Exception e) {
            System.err.println("ComputePi exception:");
            e.printStackTrace();
        }
    }
}

编译并测试客户端的代码。

compile and test the client's code.

编辑:编辑删除对rmi编译器的依赖(rmic)

edited to remove dependency on rmi compiler (rmic)

这篇关于用于远程ip(主机)的Java RMI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆