Thread: Retrieve IPv4 from Struct hostent (gethostbyname)

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    Question Retrieve IPv4 from Struct hostent (gethostbyname)

    Hello,
    How can I retrieve the IPv4 address numbers-and-dots notation of a host?
    For some reason, this example always prints 0.0.0.0. What am I missing?

    Thanks in advance.
    Code:
    char* host="some host URL"
    ...
    if(!(hostInfo = gethostbyname(host))){
    	herror("error: gethostbyname"); exit(-1);
    }
    addrSrvr.sin_addr.s_addr = inet_aton(hostInfo->h_addr, &addrSrvr.sin_addr);
    printf("IP address: %s\n", inet_ntoa(addrSrvr.sin_addr));

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Can't you use intet_ntop() to get a string in the form you requested.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm not sure what you are trying to do here:

    Code:
    addrSrvr.sin_addr.s_addr = inet_aton(hostInfo->h_addr, &addrSrvr.sin_addr);
    This might be an issue since the address is already big endian.

    Try:

    Code:
    	struct in_addr *addr = (struct in_addr*)hostInfo->h_addr;
    	printf("%s\n", inet_ntoa(*addr));
    Gethostbyname() is apparently depreciated, dunno why. Maybe the static return value, and ipv6 issues for some implementations.
    Last edited by MK27; 01-20-2012 at 10:12 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Quote Originally Posted by MK27 View Post
    I'm not sure what you are trying to do here:

    Code:
    addrSrvr.sin_addr.s_addr = inet_aton(hostInfo->h_addr, &addrSrvr.sin_addr);
    This might be an issue since the address is already big endian.

    Try:

    Code:
    	struct in_addr *addr = (struct in_addr*)hostInfo->h_addr;
    	printf("%s\n", inet_ntoa(*addr));
    Gethostbyname() is apparently depreciated, dunno why. Maybe the static return value, and ipv6 issues for some implementations.
    Hmmmm...With this I get the correct IP. I guess I should have thought about your solution , thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bcopy & hostent struct
    By Annonymous in forum Networking/Device Communication
    Replies: 6
    Last Post: 06-26-2011, 07:45 AM
  2. IPv4 is no more
    By Salem in forum General Discussions
    Replies: 12
    Last Post: 02-04-2011, 07:32 AM
  3. geting ipv4 and ipv6 interfaces with ioclt
    By wwwnuwan in forum Networking/Device Communication
    Replies: 0
    Last Post: 04-21-2009, 12:38 AM
  4. Sockets: IPv4 vs IPv6
    By ninboy in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-15-2008, 01:02 AM
  5. pointers, hostent and gethostbyname
    By whackaxe in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-06-2004, 01:57 PM