Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Lookup IP address from hostname (C/socket)

Posted on 1999-07-15
3
Medium Priority
?
11,225 Views
Last Modified: 2013-12-23
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("someserver.com");

  ... get IP address ...

  memset (&sa, '\0', sizeof(sa));
  sa.sin_family      = AF_INET;
  sa.sin_addr.s_addr=inet_addr(ipaddress);
  sa.sin_port        = 80;          

Thanks,
pluim.
0
Comment
Question by:pluim
[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
3 Comments
 

Expert Comment

by:msmanju
ID: 1583896
try this

sa.sin_addr.s_addr= *((unsigned long *)host->h_addr);
0
 
LVL 4

Author Comment

by:pluim
ID: 1583897
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.

0
 
LVL 1

Accepted Solution

by:
alextr earned 400 total points
ID: 1583898
struct hostent *hp;
 struct in_addr in;

hp = gethostbyname(argv[1]);

 (void) memcpy(&in.s_addr, *(hp->h_addr_list), sizeof (in.s_addr));
 (void) printf("%s\n", inet_ntoa(in));

0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

705 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