简体   繁体   中英

RMI : determine the IP address of a remote object

I have functioning RMI client-server system. 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. Now I am asked to display the clients' IP address in this interface.

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 ?

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.

RMISecurityManager and the UnicastRemoteObject don't say anything about that.
You can use the RemoteServer.getClientHost when the client connects.
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. 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.

I am not sure how reliable the method is, as it contains a lot of instanceof s. However I successfully tested it in a LAN where I used TCP connections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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