Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>127.0.0.1 is the loopback adapter - it's a perfectly correct response to the (somewhat malfomed) question "what is my IP address?"</p> <p>The problem is that there are <em>multiple</em> correct answers to that question.</p> <p>EDIT: The docs for <code>getLocalHost</code> say:</p> <blockquote> <p>If there is a security manager, its checkConnect method is called with the local host name and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, an InetAddress representing the loopback address is returned.</p> </blockquote> <p>Is it possible that the change in behaviour is due to a change in permissions?</p> <p>EDIT: I believe that <code>NetworkInterface.getNetworkInterfaces</code> is what you need to enumerate all the possibilities. Here's an example which doesn't show virtual addresses, but works for "main" interfaces:</p> <pre><code>import java.net.*; import java.util.*; public class Test { public static void main(String[] args) throws Exception // Just for simplicity { for (Enumeration&lt;NetworkInterface&gt; ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) { NetworkInterface iface = ifaces.nextElement(); System.out.println(iface.getName() + ":"); for (Enumeration&lt;InetAddress&gt; addresses = iface.getInetAddresses(); addresses.hasMoreElements(); ) { InetAddress address = addresses.nextElement(); System.out.println(" " + address); } } } } </code></pre> <p>(I'd forgotten just how awful the <code>Enumeration&lt;T&gt;</code> type is to work with directly!)</p> <p>Here are the results on my laptop right now:</p> <pre><code>lo: /127.0.0.1 eth0: /169.254.148.66 eth1: eth2: ppp0: /10.54.251.111 </code></pre> <p>(I don't think that's giving away any <em>hugely</em> sensitive information :)</p> <p>If you know which network interface you want to use, call <code>NetworkInterface.getByName(...)</code> and then look at the addresses for that interface (as shown in the code above).</p>
 

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