gethostbyname

这个gethostbyname只能查ipv4的,请看getaddrinfo来查 ipv4 和 ipv6

 

#include    <stdlib.h>
#include    <stdio.h>
#include <netdb.h>
#include <sys/socket.h>

/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description: 
 * =====================================================================================
 */
int main ( int argc, char *argv[] )
{
    char           *ptr, **pptr;
    struct hostent *hptr;
    char            str[32];
   
    ptr = argv[1];

    hptr = gethostbyname(ptr);
    if (hptr == NULL)
    {
        perror("gethostbyname");
        return -1;
    }

    /* print offical name */
    printf("official hostname: %s/n", hptr->h_name);
    /* print alias */
    for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)
        printf("alias: %s/n", *pptr);
    /* print address */
    switch(hptr->h_addrtype)
    {
        case AF_INET:
        case AF_INET6:
            pptr = hptr->h_addr_list;
            for(;*pptr != NULL; pptr++)
            {
                inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str));
                printf("address: %s /n", str);
            }
            break;
        default:
            printf("Unknown address type/n");
    }

    return EXIT_SUCCESS;
}                /* ----------  end of function main  ---------- */

你可能感兴趣的:(struct,list,function,null)