This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: IP ADDRESS OF MACHINE
- From: snodx at hotmail dot com
- To: gcc-help at gcc dot gnu dot org
- Date: Wed, 27 Feb 2002 18:48:26 +0530
- Subject: Re: IP ADDRESS OF MACHINE
Greetings,
Thanks to Stephano Mariani and Robert Lopez for their inputs.
Robert Lopez wrote:
>The GNU C Library Reference Manual might be useful to you. See the
sections on Host Addresses and Host Names in the Sockets section.
Where can I get the GNU C Library Reference Manual?
Stephano Mariani wrote:
>See `man gethostbyname`
I gather that you suggested that I use this function which exists in the netdb.h header file
I wrote the following C program:
#include<unistd.h>
#include<netdb.h>
main()
{
int snodsize=12;
char snodarr[100],arrsnod[100],*sndptr,*snodptr,**snodptrptr;
struct hostent *hstptr;
sndptr=snodarr;
snodptr=arrsnod;
snodptrptr=&snodptr;
gethostname(sndptr,(size_t)snodsize);
hstptr=gethostbyname(sndptr);
snodptrptr=hstptr->h_addr_list;
printf("%s",*snodptrptr);
}
___________________________________________________________________________________________________
The program is simple to understand:
It declares a pointer to a character ARRAY called sndptr
It declares another pointer to another character array snodptr
It declares a pointer to a character pointer called snodptrptr
It retrieves the hostname of the current system using gethostname of unistd.h
It retrieves a pointer to a hostent structure using gethostbyname of netdb.h
Next I equate the pointer-to-character-pointer variable snodptrptr
to the h_addr_list field of the hostent structure. This is because
h_addr_list is the most likely field where the IP Address might be
stored and it is a pointer-to-pointer which cannot be accessed directly.
Hence snodptrptr
I display snodptrptr.
But I am not getting the IP Address as I wanted it.
__________________________________________________________________________________________________
What am I doing wrong? Are there any modifications in the code?
SNODX