title: Get local IP-Address without connecting to the internet
body: <p>So, I'm trying to get the IP-Address of my machine in my local network (which should be <code>192.168.178.41</code>).</p>
  
  <p>My first intention was to use something like this:</p>
  
  <pre><code>InetAddress.getLocalHost().getHostAddress();
  </code></pre>
  
  <p>but it only returns <code>127.0.0.1</code>, which is correct but not very helpful for me.</p>
  
  <p>I searched around and found this answer <a href="https://stackoverflow.com/a/2381398/717341">https://stackoverflow.com/a/2381398/717341</a>, which simply creates a <code>Socket</code>-connection to some web-page (e.g. "google.com") and gets the local host address from the socket:</p>
  
  <pre><code>Socket s = new Socket("google.com", 80);
  System.out.println(s.getLocalAddress().getHostAddress());
  s.close();
  </code></pre>
  
  <p>This does work for my machine (it returns <code>192.168.178.41</code>), but it needs to connect to the internet in order to work. Since my application does not require an internet connection and it might look "suspicious" that the app tries to connect to google every time it is launched, I don't like the idea of using it.</p>
  
  <p>So, after some more research I stumbled across the <code>NetworkInterface</code>-class, which (with some work) does also return the desired IP-Address:</p>
  
  <pre><code>Enumeration&lt;NetworkInterface&gt; interfaces = NetworkInterface.getNetworkInterfaces();
  while (interfaces.hasMoreElements()){
      NetworkInterface current = interfaces.nextElement();
      System.out.println(current);
      if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue;
      Enumeration&lt;InetAddress&gt; addresses = current.getInetAddresses();
      while (addresses.hasMoreElements()){
          InetAddress current_addr = addresses.nextElement();
          if (current_addr.isLoopbackAddress()) continue;
          System.out.println(current_addr.getHostAddress());
      }
  }
  </code></pre>
  
  <p>On my machine, this returns the following:</p>
  
  <pre><code>name:eth1 (eth1)
  fe80:0:0:0:226:4aff:fe0d:592e%3
  192.168.178.41
  name:lo (lo)
  </code></pre>
  
  <p>It finds both my network interfaces and returns the desired IP, but I'm not sure what the other address (<code>fe80:0:0:0:226:4aff:fe0d:592e%3</code>) means.</p>
  
  <p>Also, I haven't found a way to filter it from the returned addresses (by using the <code>isXX()</code>-methods of the <code>InetAddress</code>-object) other then using RegEx, which I find very "dirty".</p>
  
  <p>Any other thoughts than using either RegEx or the internet?</p>