Tag:
hostent

Display hostname and IP address in C

by
Ryan
on
October 18, 2008

Here is a good way to determine the hostname and IP address of the local machine in C.
You first have to grab the hostname with gethostname().

char hostbuf[256];
gethostname(hostbuf,sizeof(hostbuf));

Take the hostname and use it to grab the hostent struct with gethostbyname().

struct hostent *hostentry;
hostentry = gethostbyname(hostbuf);

Finally you have to take the hostent that is returned and pull out [...]

Read More
3 Comments
C
Copyright 2008-2010 WiredRevolution.com. All rights reserved.