title: Get Local IP-Address using Boost.Asio
body: <p>I'm currently searching for a portable way of getting the local IP-addresses. Because I'm using Boost anyway I thought it would be a good idea to use Boost.Asio for this task.</p>
  
  <p>There are several examples on the net which should do the trick. Examples:</p>
  
  <p><a href="http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/overview/networking/protocols.html" rel="noreferrer">Official Boost.Asio Documentation</a></p>
  
  <p><a href="http://hi.baidu.com/xshl5/blog/item/bebd0ee9df347f3ab80e2d8c.html" rel="noreferrer">Some Asian Page</a></p>
  
  <p>I tried both codes with just slight modifications. The Code on Boost.Doc was changed to not resolve "www.boost.org" but "localhost" or my hostname instead. For getting the hostname I used boost::asio::ip::host_name() or typed it directly as a string.</p>
  
  <p>Additionally I wrote my own code which was a merge of the above examples and my (little) knowledge I gathered from the Boost Documentation and other examples.</p>
  
  <p>All the sources worked, but they did just return the following IP:<br/>
  127.0.1.1 (That's not a typo, its .1.1 at the end)<br/>
  I run and compiled the code on Ubuntu 9.10 with GCC 4.4.1</p>
  
  <p>A colleague tried the same code on his machine and got<br/>
  127.0.0.2 (Not a typo too...)<br/>
  He compiled and run on Suse 11.0 with GCC 4.4.1 (I'm not 100% sure)<br/></p>
  
  <p>I don't know if it is possible to change the localhost (127.0.0.1), but I know that neither me or my colleague did it. ifconfig says loopback uses 127.0.0.1. ifconfig also finds the public IP I am searching for (141.200.182.30 in my case, subnet is 255.255.0.0)</p>
  
  <p>So is this a Linux-issue and the code is not as portable as I thought? Do I have to change something else or is Boost.Asio not working as a solution for my problem at all?</p>
  
  <p>I know there are much questions about similar topics on Stackoverflow and other pages, but I cannot find information which is useful in my case. If you got useful links, it would be nice if you could point me to it.</p>
  
  <p>PS:
  Here is the modified code I used from Boost.Doc:</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  using boost::asio::ip::tcp;    
  
  boost::asio::io_service io_service;
  tcp::resolver resolver(io_service);
  tcp::resolver::query query(boost::asio::ip::host_name(), "");
  tcp::resolver::iterator iter = resolver.resolve(query);
  tcp::resolver::iterator end; // End marker.
  while (iter != end)
  {
      tcp::endpoint ep = *iter++;
      std::cout &lt;&lt; ep &lt;&lt; std::endl;
  }
  </code></pre>