2

I have a local network interfaces: 127.0.0.1, 192.168.0.73

If I use the following code:

std::string local_address = "";
std::string local_port= "80";
boost::asio::ip::tcp::resolver resolver(io_service_executors_);
ba::ip::tcp::resolver::query query(local_address, local_port);
local_endpoint_it_ = resolver.resolve(query);

for(auto it = local_endpoint_it_; it != ba::ip::tcp::resolver::iterator(); ++it)
    std::cout << it->endpoint() << std::endl;

When local_address = "", I can only see:

[:: 1]:80, 
127.0.0.1:80

When local_address = "0.0.0.0", I only see:

0.0.0.0:80

When local_address = "192.168.0.73", I see:

192.168.0.73:80

How do I get all the addresses of my local interfaces, if at this time I do not know their addresses?

An example output must be:

127.0.0.1:80, 
192.168.0.73:80 
and may be [:: 1]: 80
John Willemse
  • 6,608
  • 7
  • 31
  • 45
Alex
  • 12,578
  • 15
  • 99
  • 195
  • @John Zwinck Thanks! May be it can be done by using boost::asio (crossplatform and as usual with a little bit more friendly interface)? – Alex Jun 25 '13 at 11:59
  • People wanted the same thing five years ago too, but it seems nothing has changed: http://comments.gmane.org/gmane.comp.lib.boost.asio.user/2449 – John Zwinck Jun 25 '13 at 12:05

1 Answers1

1

There is a C++ wrapper for getifaddrs here: http://vrjuggler.org/docs/vapor/2.2/programmer.reference/namespacevpr.html#aadd07b8751f2d2ba6b757e9c11fd7eab

It's not part of Boost Asio, but that project uses Boost and implements this itself, so hopefully you'll find it suitable.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • The link is dead now, look here https://github.com/vancegroup-mirrors/vrjuggler/blob/master/modules/vapor/vpr/md/common/InetAddrHelpers.cpp – gunnerone Aug 31 '23 at 15:02