Search code examples
javarmi

RMI Server to Client call fails when Client machine has more than one IP Address


First off, I'd love to post some real code for this question, but I can't because it's far too many lines. That said, here's my situation:

Server Side

I have an RMI Server that waits for Clients to connect and "register" themselves, so that the Server can make function calls on Clients. Basically, the server has a published function that works like the following pseudo-code:

public class Server extends UnicastRemoteObject implements ServerInterface{
    public Server(){ /* Server publishes itself here */ }

    ...

    /** One of many methods visible to a remote Client */
    public void registerClient(Client c) throws RemoteException{
        //1. Make some remote calls on 'c' for book-keeping purposes
        //2. Store reference to c to make calls on it later
    }
}

Client Side

On Startup, the Client makes a call to Naming.lookup([url]) to get a stub to the Server that I'll call serverRef, then calls serverRef.registerClient(this). The problem happens the first line of the server's registerClient(Client c) method.

Problem

When the server makes its first remote method call to the Client inside the registerClient method, a RemoteException is thrown. While client and server machines are on the same subnet, the client's machine has a secondary IP address. So the IPs look something like this:

Server Machine IPs: 123.45.67.1
Client Machine IPs: 123.45.67.2, 192.168.67.2

The RemoteException that gets thrown during the Server's first remote call back to the client indicates that the Server is trying to connect to the Client's 192.168.67.2 address, which is what's causing the failure. The Server should be trying to connect to the 123.45.67.1 address. I know that disabling the network interface that belongs to the second IP address would fix the problem, but this isn't really an option for me.

Is there any way to "tell" server-side RMI which IP address to connect on when opening a connection to a new client-side object stub?


Solution

  • There are several possible solutions, including

    • write a custom client socket factory

    • specify addresses in java.rmi.server.hostname property

      -Djava.rmi.server.hostname=ip_address

    • etc

    Check out this link:

    These links are also useful: