Solved

Getting the local hostname and/or ip address on Linux

Posted on 2004-09-03
16
616 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 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
Independent Software Vendors: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is sub-make ? 2 106
Beginner to Unreal Engine 4 5 120
find Speed using GPS latitude & longitude 4 161
How to increment counter variables in ANSI  C (Not C# or C++). 9 72
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 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