pluim
asked on
Lookup IP address from hostname (C/socket)
How do I find the IP address for a given host, in C?
This is on a UNIX box (AIX), TCP/IP installed and
DNS available. Assume a hostname has no aliases and
only one associated IP address.
This is the code I have so far:
struct hostent *host;
struct sockaddr_in sa;
char ipaddress[32];
host=gethostbyname("somese rver.com") ;
... get IP address ...
memset (&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr=inet_ad dr(ipaddre ss);
sa.sin_port = 80;
Thanks,
pluim.
This is on a UNIX box (AIX), TCP/IP installed and
DNS available. Assume a hostname has no aliases and
only one associated IP address.
This is the code I have so far:
struct hostent *host;
struct sockaddr_in sa;
char ipaddress[32];
host=gethostbyname("somese
... get IP address ...
memset (&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr=inet_ad
sa.sin_port = 80;
Thanks,
pluim.
ASKER
msmanju, thanks for the answer. However, as I hoped the code would explain, I want the IP address in a readable format (hence the char ipaddress[32]).
Something in the form "1.2.3.4".
Thanks.
Something in the form "1.2.3.4".
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sa.sin_addr.s_addr= *((unsigned long *)host->h_addr);