Tutorial :Java: How do I get the IP of the local interface that can reach a remote IP?


I have a Java application that registers a server component in a service provider, and then sends the service name to a client. The client uses the service name to get an address out of the service provider to the server. However, the server has several interfaces only 1 of which the client get get at, so the service must be registered with the right IP. We discovered the client through a broadcast, so I have the client IP and an Enumeration of the computers network interfaces. How do I match the IP to an interface, not knowing what the netmask of the client IP is?

Spontaneously I imagine turning all the addresses to ints and chomping the local ips with their netmask and looking for a 'best match', but I wonder if there is a better way?

(this is an enterprise(tm) solution, so cutting out the service provider isn't an option, at least not w/o a political campaign first ;) )


By my understanding, the service provider has interfaces on multiple distinct networks. Each client is on only one of those networks and thus can only communicate with one of the interfaces. The service needs to discover which of the interfaces is accessible to the client so that it may send the correct address. You (the service developer) have no control over the network environment into which the service is deployed and thus cannot employ network-level solutions such as DNS.

In many network setups routing is symmetric. That is, the route from host A to host B is the same as that from host B to host A. If you know this to be the case in all environments to which your service will be deployed then you can safely assume that the address used to connect to the client is accessible to the client. That address could be discovered, for example, by connecting a DatagramSocket to the client and then calling its getLocalAddress() method.

If routing is asymmetric I know of no way to determine whether a particular interface is accessible to the client using only the java.net API. If you control the client implementation you might be able to have it include the broadcast source address (which should be the server interface visible to it) in its broadcast response packet. Beyond that, you haven't included enough information about the scenario to give specific advice. For example, it would be useful to know what protocol is used for the broadcast advertisement and client response thereto.


This depends on how you are going about this.

Sample code much?

There is a a ".getHostAddress()" method for InetAddress which might be useful.


This is usually done using DNS. The interface should be listed in DNS so you can reach it by name rather than address servicehost.example.com rather than


It is more a network administration issue than the application.

Consider the server (hosting the java application) is multi-homed. it can be three address: 1. (Public IP) 2. (Private, Internally assigned IP)

Which IP will your potential client can get access depends on the network configuration.

The client may access from a public IP, but it will access your private IP due to NAT. Another example, may be your client IP is, through NAT, it access your IP.

You cannot calculated the correct IP (to publish your service) by any kind of prefix matching.

You have mentioned that, it is an enterprise solution, and complex network configuration is common in such kind of environment.

With DNS, your server host may be myapp.company.com It is is up to the DNS facility to resolve the correct IP for the client. You may need to get coordinate with networking guy, with proper support in DNS, the solution should be trivial and more robust.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »