Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting the IP address of the current machine using Java
    primarykey
    data
    text
    <p>I am trying to develop a system where there are different nodes that are run on different system or on different ports on the same system. </p> <p>Now all the nodes create a Socket with a target IP as the IP of a special node known as a bootstrapping node. The nodes then create their own <code>ServerSocket</code> and start listening for connections. </p> <p>The bootstrapping node maintains a list of Nodes and returns them on being queried.</p> <p>Now what I need is the node must register its IP to the bootstrapping node. I tried using <code>cli.getInetAddress()</code> once the client connects to the <code>ServerSocket</code> of bootstrapping node but that didn't work.</p> <ol> <li>I need the client to register its PPP IP if available;</li> <li>Otherwise the LAN IP if available; </li> <li>Otherwise it must register 127.0.0.1 assuming its the same computer.</li> </ol> <p>Using the code:</p> <pre><code>System.out.println(Inet4Address.getLocalHost().getHostAddress()); </code></pre> <p>or</p> <pre><code>System.out.println(InetAddress.getLocalHost().getHostAddress()); </code></pre> <p>My PPP Connection IP address is: 117.204.44.192 but the above returns me 192.168.1.2</p> <p><strong>EDIT</strong></p> <p>I am using the following code:</p> <pre><code>Enumeration e = NetworkInterface.getNetworkInterfaces(); while(e.hasMoreElements()) { NetworkInterface n = (NetworkInterface) e.nextElement(); Enumeration ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); System.out.println(i.getHostAddress()); } } </code></pre> <p>I am able to get all the IP addresses associated all <code>NetworkInterface</code>s, but how do I distinguish them? This is the output I am getting:</p> <pre><code>127.0.0.1 192.168.1.2 192.168.56.1 117.204.44.19 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload