Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting the local hostname and/or ip address on Linux

Posted on 2004-09-03
16
Medium Priority
?
623 Views
Last Modified: 2012-05-05
Is there a simple way to get the hostname and/or ip address of the local machine in a C++ program on linux. Want to put it into a string variable.

One way I do it is that I run a System command(something like hostname>hostfile)from the program.....which writes the hostname into a file and then read from the file subsequently...but I kinda don't like it. There ought to be a better way.
0
Comment
Question by:sandeep_th
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 2
16 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 11976714
You'd use

#include <netdb.h>

char acHostname [ 255];

gethostname ( acHostname, 255);
struct hostent * pHostEnt = gethostbyname ( acHostname);

The IP address is available via

short sIP = pHostEnt->h_addr;

and can be transformed into a string using 'inet_ntoa()':

char acNameAndAddress [512];

sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa ( pHostEnt->h_addr));
0
 
LVL 30

Expert Comment

by:Axter
ID: 11976815
>>One way I do it is that I run a System command(something like hostname>hostfile)from the
>>program.....which writes the hostname into a file and then read from the file subsequently...but I
>>kinda don't like it. There ought to be a better way.

jkr's method is the right approach.  But in the future, if you ever need to get the out put of a command, you don't have to put it into a file.
Instead you can use popen function.

Example:

FILE *fp = popen("hostname", "rt");
char acHostname [999];
fgets(acHostname , sizeof(acHostname ), fp);
0
 
LVL 30

Expert Comment

by:Axter
ID: 11976840
I forgot to mention, popen is included in stdio.h

#include <stdio.h>

see "man popen" for more info.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sandeep_th
ID: 11977013
For jkr:

On compiling I get the following errors:

ta.cpp:21: error: invalid conversion from `char*' to `short int'
ta.cpp:27: error: conversion from `char*' to non-scalar type `in_addr'

line 21: short sIP = pHostEnt->h_addr;
line 27: sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa ( pHostEnt->h_addr));

For Axter:
I get a segmentation fault on the line:
fgets(acHostname , sizeof(acHostname ), fp);









0
 
LVL 86

Expert Comment

by:jkr
ID: 11977085
Sorry, that should be

in_addr iaIP = pHostEnt->h_addr;
sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa ( iaIP));
0
 

Author Comment

by:sandeep_th
ID: 11977160
Made no difference to the errors.

I did get the general drift though.

Here's one simple way I found for getting the hostname:

#include <unistd.h>

int gethostname(const char *name, int namelen);

If there is a similar one for the IP address....or to translate the ip address from the given hostname.....then this one is probably the simplest way.

0
 
LVL 86

Expert Comment

by:jkr
ID: 11977203
>>Here's one simple way I found for getting the hostname

Um, take a look at my 1st comment... line 3.

OK, so another try for that :o)

in_addr iaIP;
bcopy(iaIP, pHostEnt->h_addr, sizeof ( in_addr));
0
 

Author Comment

by:sandeep_th
ID: 11977338
right....sorry....I should have said "another simple way" :-)

The last line u sent also doesn't work :-(
0
 
LVL 86

Expert Comment

by:jkr
ID: 11977385
Ooops :o)

in_addr iaIP;
bcopy(&iaIP, pHostEnt->h_addr, sizeof ( in_addr));

It's getting late here...
0
 

Author Comment

by:sandeep_th
ID: 11977478
Hey...it works finally....sorta....'cause it returned a strange IP address:48.17.48.64
which is not even remotely close to the actual one!!!

The final code I used:

char acHostname [ 255];
gethostname ( acHostname, 255);
struct hostent * pHostEnt = gethostbyname ( acHostname);
char acNameAndAddress [512];
in_addr iaIP;
bcopy(&iaIP, pHostEnt->h_addr, sizeof ( in_addr));
sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa ( iaIP));
cout<<"HOSTNAME:"<<acHostname<<endl;
cout<<"IP ADDRESS:"<<acNameAndAddress<<endl;

0
 
LVL 86

Expert Comment

by:jkr
ID: 11977492
Have you verified that with ifconfig?
0
 

Author Comment

by:sandeep_th
ID: 11977506
Yep.....haven't a clue where it came from!! It's not as if even a part of it is right. It is completely different from the real ip address.
0
 

Author Comment

by:sandeep_th
ID: 11977563
.....both internal and external
0
 

Author Comment

by:sandeep_th
ID: 11979850
Ok...this was the erroneous line:

sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa ( iaIP));

should be replaced by:

sprintf ( acNameAndAddress, "%s\t%s", acHostname, inet_ntoa(*((struct in_addr *)pHostEnt->h_addr)));

cheers

0
 

Author Comment

by:sandeep_th
ID: 11980167
I'd sure like to know more about Axter's approach...with these lines of code:

FILE *fp = popen("hostname", "rt");
char acHostname [999];
fgets(acHostname , sizeof(acHostname ), fp);

And why it gave a segmentation fault on the following line :
fgets(acHostname , sizeof(acHostname ), fp);

Would be good to know
0
 

Author Comment

by:sandeep_th
ID: 11980209
Ok....got the error in Axter's code also, the "HOW" of it:

FILE *fp = popen("hostname", "rt");

ought to be replaced by:

FILE *fp = popen("hostname", "r");


Now could anyone shed some light on the "WHY" of it?


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question