使用Boost.Asio获取本地IP地址 [英] Get Local IP-Address using Boost.Asio

查看:537
本文介绍了使用Boost.Asio获取本地IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻找一种获取本地IP地址的便携式方法.因为无论如何我都在使用Boost,所以我认为将Boost.Asio用于此任务是一个好主意.

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.

网上有几个应该可以解决问题的例子.例子:

There are several examples on the net which should do the trick. Examples:

Boost.Asio官方文档

某些亚洲网页

我只是稍作修改就尝试了这两个代码. Boost.Doc上的代码已更改为不解析"www.boost.org",而是解析"localhost"或我的主机名.为了获得主机名,我使用了boost :: asio :: ip :: host_name()或直接将其键入为字符串.

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.

此外,我还编写了自己的代码,这些代码是上述示例与我从Boost文档和其他示例中收集到的(一点)知识的结合.

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.

所有来源都有效,但它们确实返回了以下IP:
127.0.1.1(这不是错字,结尾是.1.1)
我在带有GCC 4.4.1的Ubuntu 9.10上运行并编译了代码

All the sources worked, but they did just return the following IP:
127.0.1.1 (That's not a typo, its .1.1 at the end)
I run and compiled the code on Ubuntu 9.10 with GCC 4.4.1

一个同事在他的机器上尝试了相同的代码并得到了
127.0.0.2(也不是错字...)
他使用GCC 4.4.1编译并在Suse 11.0上运行(我不确定100%确定)

A colleague tried the same code on his machine and got
127.0.0.2 (Not a typo too...)
He compiled and run on Suse 11.0 with GCC 4.4.1 (I'm not 100% sure)

我不知道是否可以更改本地主机(127.0.0.1),但是我知道我和我的同事都没有这样做. ifconfig表示回送使用127.0.0.1. ifconfig还会找到我要搜索的公共IP(在我的情况下为141.200.182.30,子网为255.255.0.0)

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)

那么这是Linux发行的,并且代码不像我想象的那样可移植吗?我是否需要更改其他内容,或者Boost.Asio根本无法解决我的问题?

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?

我知道关于Stackoverflow和其他页面上类似主题的问题很多,但是我找不到对我的情况有用的信息.如果您有有用的链接,请告诉我.

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.

PS: 这是我在Boost.Doc中使用的修改后的代码:

PS: Here is the modified code I used from Boost.Doc:

#include <boost/asio.hpp>
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 << ep << std::endl;
}

推荐答案

这是我从python网络编程(google)中学到的窍门,用于弄清机器的ip地址.仅当您具有互联网连接并且可以连接到google.com且确实为我提供了我的家用计算机的192.168.x.x专用地址时,这种方法才有效.

Here's a trick I learned from python network programming (google) to figure out my machine's ip address. This only works if you have an internet connection and can connect to google.com and does give me my home machine's 192.168.x.x private address.

try {
    boost::asio::io_service netService;
    udp::resolver   resolver(netService);
    udp::resolver::query query(udp::v4(), "google.com", "");
    udp::resolver::iterator endpoints = resolver.resolve(query);
    udp::endpoint ep = *endpoints;
    udp::socket socket(netService);
    socket.connect(ep);
    boost::asio::ip::address addr = socket.local_endpoint().address();
    std::cout << "My IP according to google is: " << addr.to_string() << std::endl;
 } catch (std::exception& e){
    std::cerr << "Could not deal with socket. Exception: " << e.what() << std::endl;

 }

这篇关于使用Boost.Asio获取本地IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆