simple gethostbyname example

Here’s a simple example using gethostbyname() that emulates the host command. I wrote it to debug a DNS problem. (On the network I’m on right now, gethostbyname() returns a different answer than host and dig. It is overagressively caching other hostnames. i.e. paulloookup shrub.ca is returning 17.250.248.64, which is mail.mac.com (!)

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    if (
argc < 2) {
        
printf("Usage: %s hostname", argv[0]);
        exit(-
1);
    }

    struct hostent *hp = gethostbyname(argv[1]);

    if (hp == NULL) {
       
printf("gethostbyname() failed\n");
    } else {
       
printf("%s = ", hp->h_name);
       
unsigned int i=0;
       while (
hp -> h_addr_list[i] != NULL) {
          
printf( "%s ", inet_ntoa( *( struct in_addr*)( hp -> h_addr_list[i])));
          
i++;
       }
       
printf("\n");
    }
}

9 Responses to simple gethostbyname example

  1. Michal says:

    Thank you for the example Paul!

  2. Henri MICHEL says:

    Oops hit the return button to quickly
    3) exit (-1) doest not make much sense, what you get is the same as if you had written exit(255), since -1 representation is 0xffffffff in general on a 32bit system and the status returned to the parent process is truncated to the last 8 bits, exit(1) is what is used in general

    To summarize all those cosmetics changes I would recommend the following:

    /* for inet_ntoa() */
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    #include <netdb.h>
    
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char **argv) {
     if (argc < 2) {
       fprintf(stderr,"Usage: %s hostname\n", argv[0]);
       exit(1);
     }
    
     struct hostent *hp = gethostbyname(argv[1]);
    
     if (hp == NULL) {
       fprintf(stderr,"gethostbyname() failed\n");
       exit(1);
     } else {
       printf("%s = ", hp->h_name);
       unsigned int i=0;
       while ( hp -> h_addr_list[i] != NULL) {
         printf( "%s ", inet_ntoa( *( struct in_addr*)( hp -> h_addr_list[i])));
         i++;
       }
       printf("\n");
       exit(0);
     }
    }
    
  3. test30 says:

    Thank you, I used this for Windows, for those, who will find this page, here is what you need to get it working under Windows

    [code]
    #include
    #include
    WSADATA wsaData;
    WORD version;
    int error;
    version = MAKEWORD( 2, 0 );
    error = WSAStartup( version, &wsaData );

    if (argc < 2) {
    printf("Usage: %s hostname", argv[0]);
    exit(-1);
    }[/code]

  4. Pingback: Can’t obtain local IP using gethostbyname() | SeekPHP.com

  5. adeel shafi says:

    i need the program for the gethostbyname

  6. Kevin says:

    The code for Windows (based on previous responses to this post) is below. Remember to add -lws2_32 to the linker arguments. I compiled using MinGW using Eclipse on Windows 7.


    #include <stdio.h>
    #include <winsock2.h>

    WSADATA wsaData;
    WORD version;
    int error;

    int main(int argc, char **argv) {

    if (argc < 2) {
    fprintf(stderr,"Usage: %s hostname\n", argv[0]);
    exit(1);
    }

    struct hostent *hp = gethostbyname(argv[1]);
    if (hp == NULL) {
    fprintf(stderr,"gethostbyname() failed\n");
    exit(1);
    } else {
    printf("%s = ", hp->h_name);
    unsigned int i=0;
    while ( hp -> h_addr_list[i] != NULL) {
    printf( "%s ", inet_ntoa( *( struct in_addr*)( hp -> h_addr_list[i])));
    i++;
    }
    printf("\n");
    exit(0);
    }
    }

  7. Kevin says:

    Ignore my previous post – I missed a bit out

  8. Kevin says:

    The code for Windows (based on previous responses to this post) is below. Remember to add -lws2_32 to the linker arguments. I compiled using MinGW using Eclipse on Windows 7.


    #include <stdio.h>
    #include <winsock2.h>

    WSADATA wsaData;
    WORD version;
    int error;

    int main(int argc, char **argv) {

    version = MAKEWORD( 2, 0 );
    error = WSAStartup( version, &wsaData );

    if (argc < 2) {
    fprintf(stderr,"Usage: %s hostname\n", argv[0]);
    exit(1);
    }

    struct hostent *hp = gethostbyname(argv[1]);
    if (hp == NULL) {
    fprintf(stderr,"gethostbyname() failed\n");
    exit(1);
    } else {
    printf("%s = ", hp->h_name);
    unsigned int i=0;
    while ( hp -> h_addr_list[i] != NULL) {
    printf( "%s ", inet_ntoa( *( struct in_addr*)( hp -> h_addr_list[i])));
    i++;
    }
    printf("\n");
    exit(0);
    }
    }

  9. Yin says:

    Hi Kevin,

    You mentioned “Remember to add -lws2_32 to the linker arguments. I compiled using MinGW using Eclipse on Windows 7″
    I selected the project -> Properties -> C/C== Building -> Settings -> MinGW C++ Linker -> Libraries , then at Libraries (-l) I typed “ws2_32″, then OK

    When I built project I got an errors “cannot find -lws2_32″ , I wonder what did I miss?

    Thanks,
    Yin

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>