Solved

Getting the local hostname and/or ip address on Linux

Posted on 2004-09-03
16
607 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
  • 9
  • 5
  • 2
16 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

815 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now