Introduction to the socket application function gethostbyname

Source: Internet
Author: User

Address: http://blog.csdn.net/leesphone/archive/2008/03/02/2138775.aspx

Header file:
# Include <netdb. h>
# Include <sys/socket. h>
Struct hostent * gethostbyname (const char * Name );
The input value of this function is the domain name or host name, for example, "www.google.com", "WPC
"And so on.
The output value is a hostent structure (as follows ). If the function call fails, null is returned.
Struct hostent {
Char * h_name;
Char ** h_aliases;
Int h_addrtype;
Int h_length;
Char ** h_addr_list;
};

Explain this structure, where:
Char * h_name indicates the specification name of the host. For example, www.google.com
The standard name is actually www.l.google.com
.
Char ** h_aliases indicates the host alias. Www.google.com
That is, Google's own Alias. Sometimes, some hosts may have several aliases, which are used to retrieve multiple names for their websites for ease of user memory.
Int h_addrtype indicates the Host IP Address type, whether it is IPv4 (af_inet) or IPv6 (af_inet6)
Int h_length indicates the length of the Host IP address.
Int ** h_addr_lisst indicates the IP address of the host. Note that the IP address is stored in byte sequence of the network. Do not directly use printf with the % S parameter to do this. It may be a problem. Therefore, if you really need to print this IP address, you need to call inet_ntop ().
Const char * inet_ntop (int af, const void * SRC, char * DST, socklen_t CNT ):
This function is to convert the network address structure SRC of Type af into a host-ordered string and store it in a string with the length of CNT.
This function actually returns a pointer to DST. If a function call error occurs, the return value is null.
The following is a routine with detailed comments.

# Include <netdb. h>
# Include <sys/socket. h>

# Include <stdio. h>

# Include <stdlib. h>

# Include <sys/types. h>
Int main (INT argc, char ** argv)
{
Char * PTR, ** pptr;
Struct hostent * hptr;
Char STR [32];
/* Obtain the first parameter after the command, that is, the domain name or host name to be resolved */
PTR = argv [1];
/* Call gethostbyname (). All call results exist in hptr */
If (hptr = gethostbyname (PTR) = NULL)
{
Printf ("gethostbyname error for host: % s/n", PTR );
Return 0;/* if an error occurs when gethostbyname is called, 1 is returned */
}
/* Name the host specification */
Printf ("Official hostname: % s/n", hptr-> h_name );
/* The host may have multiple aliases, and all aliases can be typed out separately */
For (pptr = hptr-> h_aliases; * pptr! = NULL; pptr ++)
Printf ("Alias: % s/n", * pptr );
/* Type the address */
Switch (hptr-> h_addrtype)
{
Case af_inet:
Case af_inet6:
Pptr = hptr-> h_addr_list;
/* Type all the addresses you just obtained. The inet_ntop () function is called */
For (; * pptr! = NULL; pptr ++)
Printf ("Address: % s/n", inet_ntop (hptr-> h_addrtype, * pptr, STR, sizeof (STR )));
Break;
Default:
Printf ("unknown address type/N ");
Break;
}
Return 0;
}

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.