Java RMI 客户端
Java RMI client side
我正在尝试使用 RMI 客户端-服务器通信。
我写了以下 class/interface:
- 接口远程接口扩展远程
- Class HelloStub 扩展 UnicastRemoteObject 实现 RemoteInterface
- Class服务器,我绑定了远程obj
Class 客户如下:
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());
}
我不明白为什么我必须使用接口 RemoteInterface 来进行查找?我不能使用 HelloStub class,这是真正的远程对象吗?
谢谢再见。
如果您知道 class 名称,您可以定义存根 class 实例而不是 RemoteInterface 并将查找结果转换为存根。
但是定义存根而不是接口有什么好处呢?
我正在尝试使用 RMI 客户端-服务器通信。 我写了以下 class/interface:
- 接口远程接口扩展远程
- Class HelloStub 扩展 UnicastRemoteObject 实现 RemoteInterface
- Class服务器,我绑定了远程obj
Class 客户如下:
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()); }
我不明白为什么我必须使用接口 RemoteInterface 来进行查找?我不能使用 HelloStub class,这是真正的远程对象吗?
谢谢再见。
如果您知道 class 名称,您可以定义存根 class 实例而不是 RemoteInterface 并将查找结果转换为存根。
但是定义存根而不是接口有什么好处呢?