【问题标题】:connecting client-server RMI连接客户端-服务器 RMI
【发布时间】:2013-10-30 16:05:23
【问题描述】:

如果服务器是远程的(与客户端不在同一台机器上),我对客户端如何实际连接服务器感到困惑。我的代码使用 localhost 可以正常工作,但我无法弄清楚客户端实际上是如何连接到服务器主机的,以便它查找 rmiregistry。我对存储在服务器注册表中的内容感到困惑,它是 Sample 还是 localhost?这可能很愚蠢,但我尝试在客户端将 localhost 转换为其 ipaddress 并执行 String url = "//" + server + ":" + 1099 + "/Sample"; server 是来自 getbyname() 的 ip,但我得到一个异常:java.rmi.NotBoundException: 127.0.0.1:1099/Sample 那是两台机器上的客户端和服务器。我只是想弄清楚两者是如何远程连接的,但它甚至无法使用 localhost 的 IP 地址在同一台机器上工作。

客户:

 import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RemoteException;

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



            String url = "//" + "localhost" + ":" + 1099 + "/Sample";

            SampleInterface sample = (SampleInterface)Naming.lookup(url);



        } catch(Exception e) {
            System.out.println("SampleClient exception: " + e);
        }
    }
}

服务器:

  import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;

public class SampleServer {
    public static void main(String args[]) throws IOException {

        // Create and install a security manager
        if (System.getSecurityManager() == null)
            System.setSecurityManager(new RMISecurityManager());
        try {

            String url = "//localhost:" + 1099 + "/Sample";
            System.out.println("binding " + url);
            Naming.rebind(url, new Sample());
            // Naming.rebind("Sample", new Sample());
            System.out.println("server " + url + " is running...");
        }
        catch (Exception e) {
            System.out.println("Sample server failed:" + e.getMessage());
        }
    }
}

【问题讨论】:

    标签: java rmi


    【解决方案1】:

    服务器应绑定到在“localhost”上运行的注册表。

    客户端应在服务器主机上查找注册表。

    就这么简单。

    我对存储在服务器注册表中的内容感到困惑,是 Sample 还是 localhost?

    两者都没有。你混淆了三个不同的东西:

    1. 主机名,在本例中为“localhost”。
    2. 绑定名称,在本例中为“Sample”。
    3. 绑定的对象,即远程存根。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 2014-07-04
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      相关资源
      最近更新 更多