简体   繁体   English

RMI:确定远程对象的IP地址

[英]RMI : determine the IP address of a remote object

I have functioning RMI client-server system. 我有功能RMI客户端 - 服务器系统。 The server's host information is known to the clients so they can look it up and make calls. 客户端知道服务器的主机信息,因此他们可以查找并拨打电话。 On first call to the server, the clients pass the server a remote reference of "themselves" so that the server can make callbacks. 在第一次调用服务器时,客户端向服务器传递“自己”的远程引用,以便服务器可以进行回调。 The server maintains a list of the all connected clients (when a client shuts down it "unregisters" from the server, and there is a thread that periodically checks if the clients are reachable and evicts the ones that are not). 服务器维护所有连接的客户端的列表(当客户端从服务器关闭它“取消注册”时,并且有一个线程定期检查客户端是否可访问并驱逐那些不是的客户端)。

There is a graphical interface (part of a webapp) on the server that allows the users to visualize the clients that are connected at any time. 服务器上有一个图形界面(webapp的一部分),允许用户随时可视化连接的客户端。 Now I am asked to display the clients' IP address in this interface. 现在我被要求在此界面中显示客户端的IP地址。

My question : in RMI, if you have a remote reference of a remote object (a stub), is there a simple way to determine the host (DNS name or IP address) on which this remote object actually lives ? 我的问题:在RMI中,如果你有一个远程对象(存根)的远程引用,有没有一种简单的方法来确定这个远程对象实际存在的主机(DNS名称或IP地址)?

Note : I could use RemoteServer.getClientHost when the client first connects and store the info, or I could implement a remote method on the client that returns the host info, but I wish to know if there is a built-in RMI way of doing it with the remote reference. 注意:当客户端首次连接并存储信息时,我可以使用RemoteServer.getClientHost ,或者我可以在返回主机信息的客户端上实现远程方法,但我想知道是否有内置的RMI方法它与远程引用。

RMISecurityManager and the UnicastRemoteObject don't say anything about that. RMISecurityManager和UnicastRemoteObject没有提及任何相关内容。
You can use the RemoteServer.getClientHost when the client connects. 您可以在客户端连接时使用RemoteServer.getClientHost
I don't believe that this information can be obtained unless it is passed in the remote method call. 除非在远程方法调用中传递,否则我不相信可以获得此信息。

Note: Do not use this, see comments. 注意:请勿使用此功能,请参阅注释。

I have the same requirement, displaying the clients' IP address in a GUI. 我有相同的要求,在GUI中显示客户端的IP地址。 After debugging and analyzing the types of the remote object and its sub-objects, I came up with the following code: 在调试和分析远程对象及其子对象的类型后,我想出了以下代码:

  public static String getIpAddress(Object proxy) throws IllegalArgumentException, RemoteException, NotExpectedTypeException
  {
    InvocationHandler h = Proxy.getInvocationHandler(proxy);
    if (h instanceof RemoteObjectInvocationHandler)
    {
      RemoteRef remoteRef = ((RemoteObjectInvocationHandler) h).getRef();
      if (remoteRef instanceof UnicastRef)
      {
        Endpoint ep = ((UnicastRef) remoteRef).getLiveRef().getChannel().getEndpoint();
        if (ep instanceof TCPEndpoint) { return ((TCPEndpoint) ep).getHost(); }
        throw new NotExpectedTypeException(ep.getClass().getSimpleName(), "TCPEndpoint");
      }
      throw new NotExpectedTypeException(remoteRef.getClass().getSimpleName(), "UnicastRef");
    }
    throw new NotExpectedTypeException(h.getClass().getSimpleName(), "RemoteObjectInvocationHandler");
  }

where NotExpectedTypeException is a self defined exception. 其中NotExpectedTypeException是一个自定义的异常。

I am not sure how reliable the method is, as it contains a lot of instanceof s. 我不确定该方法的可靠性,因为它包含很多instanceof However I successfully tested it in a LAN where I used TCP connections. 但是我在使用TCP连接的局域网中成功测试了它。

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

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