In Linux, the host name and other information such as gethostbyaddr () are obtained through IP addresses.

Source: Internet
Author: User
Tags domain name server
# Include <stdlib. h> # include <stdio. h> # include <netdb. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> int main (INT argc, char ** argv) {char * PTR, ** pptr; struct hostent * hptr; char STR [32]; char ipaddr [16]; struct in_addr * hipaddr = (struct in_addr *) malloc (sizeof (struct in_addr); PTR = argv [1]; printf ("0: % s \ n", PTR ); if (! Inet_aton (PTR, hipaddr) {printf ("error1 \ n"); return 1 ;}if (hptr = gethostbyaddr (hipaddr, 4, af_inet) = NULL) {h_errno; printf ("err2% s \ n", PTR); Switch (h_errno) {Case host_not_found: printf ("111 \ n"); break; // case no_address: // case no_data: printf ("112 \ n"); break; Case no_recovery: printf ("113 \ n"); break; Case try_again: printf ("115 \ n"); break;} return 1;} printf ("hostname: % s \ n", hptr-> h_name ); for (pptr = hptr-> h_alia SES; * pptr! = NULL; pptr ++) printf ("% s \ n", * pptr); Switch (hptr-> h_addrtype) {Case af_inet: Case af_inet6: pptr = hptr-> h_addr_list; For (; * pptr! = NULL; pptr ++) printf ("Address: % s \ n", inet_ntop (hptr-> h_addrtype, * pptr, STR, sizeof (STR); break; default: printf ("Default \ n"); break;} return 0;}/** // The gethostbyname () and gethostbyaddr () returned by the http://www.rosoo.net/a/201105/11535.html () the hostent structure or NULL pointer returned by the function. If an error occurs. When an error occurs, the variable h_errno stores the error code. The variable h_errno of an error may have the following values: the host specified by host_not_found is unknown. The no_address or no_data Request Name is valid, but there is no IP address. No_recovery the name server cannot be recovered. Try_again a temporary error occurs on the authoritative Domain Name Server. Please try again later. **/

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.