简体   繁体   English

Java RMI客户端

[英]Java RMI client side

I'm trying to use the RMI Client-Server communication. 我正在尝试使用RMI客户端-服务器通信。 I wrote the following class/interface: 我编写了以下类/接口:

  1. Interface RemoteInterface extends Remote 接口RemoteInterface扩展了Remote
  2. Class HelloStub extends UnicastRemoteObject implements RemoteInterface 类HelloStub扩展了UnicastRemoteObject实现RemoteInterface
  3. Class Server, which I binded the remote obj 类服务器,我绑定了远程obj
  4. Class Client as follow: 类客户端如下:

     import java.rmi.*; public class Client { public static void main(String[] args) { try { String globalName = "rmi//127.0.0.1:1099/hello"; RemoteInterface remoteObj = (RemoteInterface)Naming.lookup(globalName); System.out.println(remoteObj.SayHello()); } catch(Exception e) { System.out.println(e.getMessage()); } 

I don't understand the reason why I must ude the interface RemoteInterface to do the lookup? 我不明白为什么必须退出接口RemoteInterface进行查找的原因? Can't I use the HelloStub class, which is the real remote obj? 我不能使用HelloStub类,它是真正的远程obj?

Thanks bye. 谢谢再见。

If you know the class name, you can define Stub class instance instead of RemoteInterface and cast your lookup result to your stub. 如果知道类名,则可以定义Stub类实例而不是RemoteInterface并将查找结果转换为您的Stub。

But what benefit you will get defining stub insteadof Interface? 但是,您将获得定义存根而不是接口的什么好处?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM