[Boost-users] how to know my own IP address

599 views
Skip to first unread message

Paolo Viotti

unread,
Aug 14, 2009, 8:49:02 AM8/14/09
to boost...@lists.boost.org
Hi,

I'm using the boost library to create and manage a UDP socket.
I was wondering if there is a way to know the public address of my machine using the boost classes.
With this code

udp::endpoint thisendpoint(udp::v4(), UDPPORT);
boost::asio::ip::address myaddress = thisendpoint.address();
std::cout<< "__My IP address: " << myaddress.to_string() << std::endl;

I can only print the any_address (0.0.0.0)...
thanks,

p_viotti

Tommy Nordgren

unread,
Aug 14, 2009, 10:23:27 AM8/14/09
to boost...@lists.boost.org

> _______________________________________________

You can always adress your own machine via the special ip adress
127.0.0.1
(or the special domain localhost)
------
What is a woman that you forsake her, and the hearth fire and the home
acre,
to go with the old grey Widow Maker. --Kipling, harp song of the Dane
women
Tommy Nordgren
tommy.n...@comhem.se

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Maurizio Vitale

unread,
Aug 14, 2009, 11:01:19 AM8/14/09
to boost...@lists.boost.org
>>>>> "Tommy" == Tommy Nordgren <tommy.n...@comhem.se> writes:

Tommy> On Aug 14, 2009, at 2:49 PM, Paolo Viotti wrote:

>> Hi,
>>
>> I'm using the boost library to create and manage a UDP socket. I
>> was wondering if there is a way to know the public address of my
>> machine using the boost classes. With this code
>>
>> udp::endpoint thisendpoint(udp::v4(), UDPPORT);
>> boost::asio::ip::address myaddress = thisendpoint.address();
>> std::cout<< "__My IP address: " << myaddress.to_string() <<
>> std::endl;
>>
>> I can only print the any_address (0.0.0.0)... thanks,
>>
>> p_viotti _______________________________________________

Tommy> You can always adress your own machine via the special
Tommy> ip adress 127.0.0.1 (or the special domain localhost)

But that hardly qualifies as the 'public address' the OP was after.
That said, I've no idea about the answer...

--

Björn Karlsson

unread,
Aug 14, 2009, 1:03:55 PM8/14/09
to boost...@lists.boost.org
Hello Paolo,

> I'm using the boost library to create and manage a UDP socket.
> I was wondering if there is a way to know the public address of my
> machine using the boost classes.

Using a resolver to retrieve the address from the host name seems to do the trick:

#include <iostream>
#include <boost/asio.hpp>

int main(int argc, char** argv)
{
boost::asio::io_service io_service;

boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(boost::asio::ip::host_name(), "");
boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query);
boost::asio::ip::tcp::endpoint endpoint = *it;

std::cout << endpoint.address().to_string() << '\n';

return 0;
}

Cheers,
Bjorn Karlsson
www.skeletonsoftware.net

KTC

unread,
Aug 14, 2009, 2:19:43 PM8/14/09
to boost...@lists.boost.org
Paolo Viotti wrote:
> Hi,
>
> I'm using the boost library to create and manage a UDP socket.
> I was wondering if there is a way to know the public address of my
> machine using the boost classes.

Connect to <http://whatismyip.com/automation/n09230945.asp> and read
what it returns.

KTC

--
Only two things are infinite,
the Universe and Stupidity.
And I'm not quite sure about the former.
- Albert Einstein

dhruva

unread,
Aug 14, 2009, 10:39:25 PM8/14/09
to boost...@lists.boost.org
Hello,

> >> I'm using the boost library to create and manage a UDP socket. I
> >> was wondering if there is a way to know the public address of my

> >> machine using the boost classes. With this code
> >>
> >> udp::endpoint thisendpoint(udp::v4(), UDPPORT);
> >> boost::asio::ip::address myaddress = thisendpoint.address();
> >> std::cout<< "__My IP address: " << myaddress.to_string() <<
> >> std::endl;
> >>
> >> I can only print the any_address (0.0.0.0)... thanks,
> >>
> >> p_viotti _______________________________________________
>
> Tommy> You can always adress your own machine via the special
> Tommy> ip adress 127.0.0.1 (or the special domain localhost)

I have worked on this topic quite extensively. We had a requirement to find the global IP address of the local machine to be able to send ICMP6 packets. The packets needs to be signed by the senders IP address else machines on a network need not respond.
The problem is a machine can have multiple NICs. So, you need a mechanism to iterate all available NICs and get the associated IP addresses. In this problem domain, MS Windows wins as they have nice APIs (GetAdaptersAddresses) to do that. Coming to GNU/Linux land, you need to parse the '/proc/net/if_inet6'. So, I ended up writing code that works on windows and gnu/linux. In short, there is no easy way to do it.

Using name resolution is not an option as some machines might not have a DNS entry. It is not foolproof.

-dhruva

See the Web&#39;s breaking stories, chosen by people like you. Check out Yahoo! Buzz. http://in.buzz.yahoo.com/

Reply all
Reply to author
Forward
0 new messages