Get host-related information using the GetHostName () function and the gethostbyname () function

Source: Internet
Author: User

From http://ty1992.blog.51cto.com/7098269/1685880

GetHostName (): Returns the standard host name of the local host.

The prototype is as follows:

#include <unistd.h>

int GetHostName (char *name, size_t len);

Parameter description:

This function requires two parameters:

The receive buffer name, whose length must be len bytes or longer, is the host name that is saved.

The maximum length of the receive buffer name

return value:

If the function succeeds, 0 is returned. Returns-1 if an error occurs. The error number is stored in the external variable errno.

gethostbyname () function Description--Get IP address with domain name or hostname
     contains header file
    #include <netdb.h>
    #include <sys/ Socket.h>

     function prototype
Span style= "FONT-SIZE:16PX; Color: #993300; " >    struct hostent *gethostbyname (const char *name);
    the passed-in value of this function is the domain name or hostname, such as "www.google.cn" and so on. The outgoing value, which is a hostent structure. If the function call fails, NULL is returned.

     returns the hostent struct type pointer
Span style= "FONT-SIZE:16PX; Color: #993300; " >          

1 2 3 4 5 6 7 8    struct hostent {               char  *h_name;            /* official name of host */               char **h_aliases;         /* alias list */               int    h_addrtype;        /* host address type */               int    h_length;          /* length of address */               char **h_addr_list;       /* list of addresses */           }           #define h_addr h_addr_list[0] /* for backward compatibility */

Hostent->h_name
represents the canonical name of the host. For example Www.google.com's canonical name is actually www.l.google.com.
    
hostent->h_aliases
represents the alias of the host. Www.google.com is Google's own alias. Sometimes, some hosts may have several aliases, these, in fact, are for easy user memory and for their own site to take more names.

Hostent->h_addrtype
represents the type of host IP address, whether it is IPv4 (af_inet) or Pv6 (AF_INET6)

Hostent->h_length
represents the length of the host IP address

Hostent->h_addr_lisst
represents the host's IP address, note that this is stored in the network byte order. Do not directly use the printf with the%s parameter to hit this thing, there will be problems of WOW. So to really need to print out this IP, you need to call Inet_ntop ().

const char *inet_ntop (int af, const void *SRC, char *dst, socklen_t cnt):
This function, which is the network address structure SRC, which is type AF, is converted into a string form of a host order, stored in a string of CNT length. Returns a pointer to the DST point. If the function is called incorrectly, the return value is null.

Examples are as follows:

123456789Ten One A - - the - - - + - + A at - - - - - in - to + - the * $Panax Notoginseng - the + A #include <stdio.h>#include <sys/socket.h>#include <netdb.h>#include <unistd.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdlib.h> void handler(int sig){        printf("recv a sig=%d\n",sig);                exit(EXIT_SUCCESS);}  #define ERR_EXIT(m) \        do{ \                perror(m); \                exit(EXIT_FAILURE);\        }while(0); int main(void){        char host[100] = {0};         if(gethostname(host,sizeof(host)) < 0){                ERR_EXIT("gethostname");        }         struct hostent *hp;        if ((hp=gethostbyname(host)) == NULL){                ERR_EXIT("gethostbyname");        }         int i = 0;        while(hp->h_addr_list[i] != NULL)        {                printf("hostname: %s\n",hp->h_name);                printf("      ip: %s\n",inet_ntoa(*(struct in_addr*)hp->h_addr_list[i]));                i++;        }        return 0;}

Compile run
-----------------------------
# Gcc-o GetInfo getinfo.c
#./getinfo

Hostname:www.server1.com

ip:69.172.201.208

Note: the gethostbyname () function can obtain information if the host name is in the domain name format (such as www.server1.com, if the function is Server1), or null is returned.

This article is from the " Dance Fish " blog, please be sure to keep this source http://ty1992.blog.51cto.com/7098269/1685880

Get host-related information using the GetHostName () function and the gethostbyname () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.