简体   繁体   English

在两台不同的计算机上创建RMI应用程序时,客户端和服务器应在哪里定义接口客户端或服务器端?

[英]When creating an RMI application on two different machines Client and Server Where should we Define our Interface Client side or Server side?

I want to write an RMI application using two laptops which adds two numbers? 我想使用两台笔记本电脑编写一个RMI应用程序,将两个数字相加? i have made one laptop as my server and another laptop as my client. 我已经将一台笔记本电脑作为服务器,将另一台笔记本电脑作为客户端。 when we want to define the interface which is extending from Remote interface on which machine should i define this interface the client side or the server side? 当我们要定义从远程接口扩展的接口时,应在客户端或服务器端在哪台计算机上定义此接口? please help. 请帮忙。

i Have made an RMI application using one machine it works fine I have defined the Interface in the same package but when i work on different machines it does not work. 我已经使用一台机器制作了一个RMI应用程序,但运行良好。

public interface AdditionI extends Remote {
    public int add(int x ,int y) throws RemoteException;
}

public class Server extends UnicastRemoteObject implements AdditionI {

   public Server() throws RemoteException {}

   @Override
   public int add(int x, int y) throws RemoteException {
       return x+y;
   }

   public static void main(String ar [])throws RemoteException {
       try
       {
           Registry reg = LocateRegistry.createRegistry(2177);
           reg.rebind("Add", new Server());
           System.out.println("Server is ready");
       }
       catch(Exception e)
       {
           System.out.println("Error "+ e);
       }
   }
}



public class Client {

    public static void main(String ar[])throws RemoteException {
        try {
            Registry reg = LocateRegistry.getRegistry("localhost",2177);
            AdditionI ad = (AdditionI)reg.lookup("Add");
            System.out.println("REsult:"+(ad.add(10, 5)));
        } catch (Exception e) {
            System.out.println("Error"+e);
        }
    }

}

when i run this code on the same machine it works fine the result of the method add is displayed, but on different machine it displays the following message. 当我在同一台计算机上运行此代码时,它可以很好地显示add方法的结果,但是在另一台计算机上,它显示以下消息。

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: java.lang.ClassNotFoundException:

Where should we Define our Interface Client side or Server side? 我们应该在哪里定义接口客户端或服务器端?

Simple: you need the interface on both sides. 很简单:你需要在两侧的接口。

The Client knows that interface, and basically it is the "only thing" it knows: there is some interface that defines the behavior (the methods) that the client code can use. 客户端知道该接口,并且基本上就是它所知道的“唯一的东西”:有一些接口定义了客户端代码可以使用的行为(方法)。

The Server knows that interface and implements it. 服务器知道该接口并实现它。

That interface is the basic thing that "links" client and server (conceptually). 该接口是(概念上)“链接”客户端和服务器的基本内容。 They both know that there is some interface AdditionI . 他们俩都知道有一些接口AdditionI The client will need it so it 客户将需要它,所以它

  • first identify a service that supports that interface 首先确定支持该接口的服务
  • when such an service is found, the client knows how to invoke the corresponding add method 找到这样的服务时,客户端知道如何调用相应的add方法

The server on the other hand uses the interface to register its implementation as a service ... that clients then can call. 另一方面,服务器使用该接口将其实现注册为服务,然后客户端可以调用该服务。

Therefore, you basically have three different parts in your source code: 因此,您的源代码中基本上包含三个不同的部分:

  • common : that contains that AdditionI interface common :包含该AdditionI接口
  • client : the additional code required to identify and later use that addition service 客户端 :识别和以后使用该附加服务所需的附加代码
  • server : the additional code to implement and register the service 服务器 :实现和注册服务的附加代码

And note: that exception java.lang.ClassNotFoundException is really basic. 请注意:异常java.lang.ClassNotFoundException确实是基本的。 It tells you that the JVM running some code did not find some class. 它告诉您运行某些代码的JVM找不到某些类。

In other words: your classpath setup is somehow broken. 换句话说:您的类路径设置已被破坏。 Simply research that exception (you can find endless endless documentation about such basic things, see here for example). 只需研究该异常即可(您可以找到有关此类基本内容的无尽文档,例如,请参见此处 )。 Most likely, it boils down to: making sure that some .class files are in the classpath ... where you need them. 很有可能,归结为:确保某些.class文件位于类路径中……您需要的位置。 And the first part already tells you where which classes need to go to! 第一部分已经告诉您哪些课程需要去哪里!

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

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