# Linux - How to get my IP in C/C++



## rockballad

Hi,

I'm writing a small program to get my computer IP on LAN (e.g. 192.168.x.x, it's not 127.0.x.x). In Windows, it's simple, but I can't do it in Linux (code is quite similar). If you know about it, could you show me please?

Thanks in advance.


----------



## Squashman

I can't do it in C or C++ but a shell script would be super easy. I can do just about anything in Linux with a shell script.


Code:


ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' |
cut -d: -f2 | awk '{ print $1}'


----------



## rockballad

Thank you! It should work well! I just set some first steps on Linux, so your help is really appreciated! I'll search on how to grab the output of a commandline or something like that. But, I think indeed that must be a way to do in C/C++. 

Cheers,


----------



## lotuseclat79

Squashman said:


> I can't do it in C or C++ but a shell script would be super easy. I can do just about anything in Linux with a shell script.
> 
> 
> Code:
> 
> 
> ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' |
> cut -d: -f2 | awk '{ print $1}'


Hi Squashman,

Your example doesn't work correctly as it needs to consider the context of whether a user's connection is dialup or not:

if a user is dialup then the following should work:
ifconfig ppp0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

otherwise,

your scripting produces the following on a dialup system with an ethernet card (unplugged):
eth0:avah: <n.n.n.n>
ppp0: <the correct ip address>

-- Tom


----------



## lotuseclat79

rockballad said:


> Thank you! It should work well! I just set some first steps on Linux, so your help is really appreciated! I'll search on how to grab the output of a commandline or something like that. But, I think indeed that must be a way to do in C/C++.
> 
> Cheers,


Hi rockballad,

One way to feed a value in your computing environment is to assign the found ip address to an environment variable, and then read the environment variable into the c/c++ program.

-- Tom


----------



## Squashman

lotuseclat79 said:


> Hi Squashman,
> 
> Your example doesn't work correctly as it needs to consider the context of whether a user's connection is dialup or not:
> 
> if a user is dialup then the following should work:
> ifconfig ppp0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
> 
> otherwise,
> 
> your scripting produces the following on a dialup system with an ethernet card (unplugged):
> eth0:avah: <n.n.n.n>
> ppp0: <the correct ip address>
> 
> -- Tom


Yes, but he asked for is LAN IP.



rockballad said:


> Hi,
> 
> *I'm writing a small program to get my computer IP on LAN (e.g. 192.168.x.x,* it's not 127.0.x.x). In Windows, it's simple, but I can't do it in Linux (code is quite similar). If you know about it, could you show me please?
> 
> Thanks in advance.


----------



## lotuseclat79

Ooops, missed that!

-- Tom


----------



## Squashman

lotuseclat79 said:


> Ooops, missed that!
> 
> -- Tom


Don't Sweat it. You are still the Linux Guru around here since Code Jockey hasn't been around much.


----------



## windows user

Call gethostname() to get your hostname and then call gethostbyname() to get your IP address. See http://beej.us/guide/bgnet/output/html/multipage/gethostbynameman.html


----------



## rockballad

Thank you all.



windows user said:


> Call gethostname() to get your hostname and then call gethostbyname() to get your IP address. See http://beej.us/guide/bgnet/output/html/multipage/gethostbynameman.html


Yes, it's expected to work. But it's not  The func gethostbyname("localhost") returns "127.0.0.1" only. And, gethostbyname("mycomputername") returns "127.0.1.1".

From the article, I though that why I don't try to get the *real* host from the IP (that I expect to get).

The command "ifconfig" in Terminal let me know that the IP on LAN is "192.168.1.5". So by using


Code:


inet_aton("192.168.1.5", &addr);
h = gethostbyaddr(&addr, sizeof addr, AF_INET);

I know the *real* host name is "mycomputername.local"! That's it. The difference is the tail ".local". Could any one tell me more about this tail and others? Maybe this's the difference between Linux and Windows, isn't it?

Thanks again! :up:


----------



## flowerbed

My Microsoft ME 2000 is not locking onto IP address. I have no Internet access. Please help.


----------



## lotuseclat79

Hi flowerbed,

Welcome to TSG!

You need to read the forum rules.

Then use the New Thread button at the top of the Forum webpage to open up your own new thread.

-- Tom


----------



## windows user

Post your code. Here's what I did and it worked. I copied and pasted from Beej's tutorial:


Code:


#include <sys/unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>

int main()
{
 char hostname[128];
 int i;
 struct hostent *he;
 struct in_addr **addr_list;
 struct in_addr addr;

 gethostname(hostname, sizeof hostname);
 printf("My hostname: %s\n", hostname);

 he = gethostbyname(hostname);

 if (he == NULL) { // do some error checking
   herror("gethostbyname"); // herror(), NOT perror()
   return 1;
 }

 // print information about this host:
 printf("Official name is: %s\n", he->h_name);
 printf("IP address: %s\n", inet_ntoa(*(struct in_addr*)he->h_addr));
 printf("All addresses: ");
 addr_list = (struct in_addr **)he->h_addr_list;

 for(i = 0; addr_list[i] != NULL; i++) {
   printf("%s ", inet_ntoa(*addr_list[i]));
 }

 printf("\n");

 return 0;
}

Output:

My hostname: kara
Official name is: kara
IP address: <something that isn't 127.0.0.1>
All addresses: <something that isn't 127.0.0.1>


----------



## tomdkat

windows user said:


> Call gethostname() to get your hostname and then call gethostbyname() to get your IP address.


Yep, that's exactly what I was going to suggest. Plus, this should work across platforms as well.

Peace...


----------



## tomdkat

rockballad said:


> I know the *real* host name is "mycomputername.local"! That's it. The difference is the tail ".local". Could any one tell me more about this tail and others? Maybe this's the difference between Linux and Windows, isn't it?


It relates to how your local hosts file is setup along with the DNS configuration. In your local hosts file, you will have an entry for "localhost" and possibly for your actual local host name. This is why gethostname() is called first, so you can get your actual host name. Then you call gethostbyname() to get the IP address.

So, I imagine on your system, gethostname() returns "mycomputername.local". You then pass this to gethostbyname() to get your IP address, as described in the tutorial linked to above.

Attached is a screenshot of the hosts file for my Gentoo Linux system running in VirtualBox on Windows XP, as an example.

Peace...


----------



## rockballad

windows user said:


> Post your code. Here's what I did and it worked. I copied and pasted from Beej's tutorial:
> 
> 
> Code:
> 
> 
> #include <sys/unistd.h>
> #include <sys/socket.h>
> #include <netdb.h>
> #include <stdio.h>
> 
> int main()
> {
> char hostname[128];
> int i;
> struct hostent *he;
> struct in_addr **addr_list;
> struct in_addr addr;
> 
> gethostname(hostname, sizeof hostname);
> printf("My hostname: %s\n", hostname);
> 
> he = gethostbyname(hostname);
> 
> if (he == NULL) { // do some error checking
> herror("gethostbyname"); // herror(), NOT perror()
> return 1;
> }
> 
> // print information about this host:
> printf("Official name is: %s\n", he->h_name);
> printf("IP address: %s\n", inet_ntoa(*(struct in_addr*)he->h_addr));
> printf("All addresses: ");
> addr_list = (struct in_addr **)he->h_addr_list;
> 
> for(i = 0; addr_list[i] != NULL; i++) {
> printf("%s ", inet_ntoa(*addr_list[i]));
> }
> 
> printf("\n");
> 
> return 0;
> }
> 
> Output:
> 
> My hostname: kara
> Official name is: kara
> IP address: <something that isn't 127.0.0.1>
> All addresses: <something that isn't 127.0.0.1>


Yeah, it should be the same result. But that's not what I need, I need the LAN IP, such as 192.168.1.5, not 127.0.0.1. So that is what I meant by


rockballad said:


> Yes, it's expected to work. But it's not! The func gethostbyname("localhost") returns "127.0.0.1" only. And, gethostbyname("mycomputername") returns "127.0.1.1".





tomdkat said:


> It relates to how your local hosts file is setup along with the DNS configuration. In your local hosts file, you will have an entry for "localhost" and possibly for your actual local host name. This is why gethostname() is called first, so you can get your actual host name. Then you call gethostbyname() to get the IP address.
> 
> So, I imagine on your system, gethostname() returns "mycomputername.local". You then pass this to gethostbyname() to get your IP address, as described in the tutorial linked to above.
> 
> Attached is a screenshot of the hosts file for my Gentoo Linux system running in VirtualBox on Windows XP, as an example.
> 
> Peace...


Thanks, I just wonder why the ".local" appears here. My /etc/hosts file is simple:


> 127.0.0.1	localhost
> 127.0.1.1	mycomputername
> 
> # The following lines are desirable for IPv6 capable hosts
> ::1 ip6-localhost ip6-loopback
> fe00::0 ip6-localnet
> ff00::0 ip6-mcastprefix
> ff02::1 ip6-allnodes
> ff02::2 ip6-allrouters
> ff02::3 ip6-allhosts


----------



## tomdkat

rockballad said:


> Thanks, I just wonder why the ".local" appears here. My /etc/hosts file is simple:


That entry in your hosts file for "mycomputername" is why you got 127.0.1.1 back instead of 127.0.0.1. I'm not sure where the ".local" is coming from but you can issue a "nslookup" on that hostname and see what it returns, in terms of from where the IP is coming.

Peace...


----------



## windows user

My program should give you the LAN IP, not the IP of the loopback interface (127.0.0.1). I'm not on a linux machine, so I can't say what you're doing wrong. I guess what tmpdkat saying is correct.


----------



## tomdkat

windows user said:


> My program should give you the LAN IP, not the IP of the loopback interface (127.0.0.1).


I think it will too. Hopefully, he can compile and run your program and see what it does on his machine. I'll do the same on mine and post the results here.

Peace...


----------



## rockballad

windows user said:


> My program should give you the LAN IP, not the IP of the loopback interface (127.0.0.1). I'm not on a linux machine, so I can't say what you're doing wrong. I guess what tmpdkat saying is correct.


Me, too. On Windows, it gives me the LAN IP, just when moving to Linux, I meet that problem.

"nslookup localhost" or "nslookup mycomputername" returns the same, it's like Internet IP, not LAN IP,


----------



## tomdkat

rockballad said:


> "nslookup localhost" or "nslookup mycomputername" returns the same, it's like Internet IP, not LAN IP,


I mean run a nslookup on "mycomputername.local". 

I've found where the ".local" comes into play. Apparently, it's part of Multicast DNS (mDNS) (see chapter 3). mDNS is related to ZeroConf.

So, specifying "hostname.local" gets resolved via mDNS to the external IP of the machine. I don't know what happens in a multi-homed configuration.

I just learned something new! 

Peace...


----------



## rockballad

tomdkat said:


> I just learned something new!
> 
> Peace...


Yeah! That's what we achieve :up:

Thanks!


----------

