Re: How to get my own IP address without DNS

From: Jose Maria Lopez Hernandez (
Date: 06/01/05

Date: Wed, 01 Jun 2005 12:05:55 +0200

Kazu wrote:
> Hi.
> I am trying to get my own IP address. I know that there is a way to use
> gethostname and gethostbyname, but this has a problem.
> 1. I cannot use this if no DNS is specified.
> 2. If several IP addresses are given for my domain name and
> the ordering is round robin, I cannot tell which one is my real IP address.
> Is there any way to get my own IP address in C or C++ without using DNS?
> (such as ifconfig)

The easiest way is to parse the /etc/hosts file, that your system
should use if there's no DNS server.

> Kazuh


Jose Maria Lopez Hernandez
Director Tecnico de bgSEC
bgSEC Seguridad y Consultoria de Sistemas
The only people for me are the mad ones -- the ones who are mad to live,
mad to talk, mad to be saved, desirous of everything at the same time,
the ones who never yawn or say a commonplace thing, but burn, burn, burn
like fabulous yellow Roman candles.
                -- Jack Kerouac, "On the Road"