struct Hostent{char *h_name;/* Host's official name */char **h_aliases;/* Host alias list */int H_addrtype;/* Host address type af_inet */int; * The address length of the host */char **h_addr_list; /* Host's IP Address list */} #define H_ADDR h_addr_list[0]/* The first IP address of the host */
struct hostent *gethostbyname (const char *name);
Obtain information such as host IP through hostname name. A hostent struct pointer that successfully returns the host name and address information for the given host name.
Examples are as follows:
#include <stdio.h> #include <netdb.h> #include <stdlib.h>int main (int argc, char *argv[]) {if (argc! = 2) {printf ("Usage:%s domain_name \ n", argv[0]); exit (exit_failure);} struct Hostent *ht = gethostbyname (argv[1]), if (HT) {int i = 0;printf ("Source domain:%s \ n", argv[1]);p rintf ("Formal DOMA In:%s \ n ", Ht->h_name);p rintf (" Address type:%s \ n ", Ht->h_addrtype = = af_inet? "Af_inet": "Af_inet6");p rintf ("Address length:%d \ n", Ht->h_length), for (;; ++i) {if (Ht->h_aliases[i]) printf (" Aliases name%d:%s \ n ", I+1, Ht->h_aliases[i]); elsebreak;} for (i=0; ++i) {if (Ht->h_addr_list[i]) printf ("IP Address%d:%s \ n", I+1, Inet_ntoa ((unsigned int *) Ht->h_addr_lis T[i])); elsebreak;}} return 0;}
struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type);
Returns a hostent structure pointer to the containing host name and address information for the given address.
Addr pointer to the network byte-order address.
The length of the Len address.
Type of address.
Examples are as follows:
#include <stdio.h> #include <netdb.h> #include <stdlib.h> #define Addr_len 4#define addr_typeaf_ Inetint Main (int argc, char *argv[]) {if (argc! = 2) {printf ("Usage:%s IP address\n", argv[0]); exit (exit_failure);} struct IN_ADDR *addr = (struct in_addr *) malloc (sizeof (struct in_addr)), if (! Inet_aton (argv[1], addr)) {printf ("%s inval ID address ", argv[1]); exit (exit_failure);} struct Hostent *ht = gethostbyaddr (addr, Addr_len, Addr_type), if (HT) {int i = 0;printf ("Source IP:%s\n", argv[1]);p rintf (" Formal domain:%s \ n ", Ht->h_name);p rintf (" Address type:%s\n ", Ht->h_addrtype = = af_inet? "Af_inet": "Af_inet6");p rintf ("Address length:%d \ n", Ht->h_length), for (;; ++i) {if (Ht->h_aliases[i]) printf (" Aliases name%d:%s\n ", I+1,ht->h_aliases[i]); elsebreak;} for (i = 0;; ++i) {if (Ht->h_addr_list[i]) printf ("IP Address%d:%s\n", I+1, Inet_ntoa ((unsigned int *) Ht->h_addr_lis T[i])); elsebreak;}} Else{switch (H_errno) {case Host_not_found:p rintf ("The specified HOST is unknowN\n "); Break;case no_address:printf (" The requested name is valid and does not has an IP address.\n "); Break;case No_recover Y:printf ("A nonrecoverable name server error occurred.\n"); Break;case try_again:printf ("A temporary error occurred in the Authoritative domain name server. Please try again later.\n "); return 0;}
The gethostbyname () and gethostbyaddr () functions return the structure of the hostent or null pointers.
If an error occurs. The H_errno variable holds the error number.
The variable h_errno of the error can have the following values:
Host_not_found
The specified host is unknown.
No_address or No_data
The name of the request is valid, but does not have an IP address.
No_recovery
The unrecoverable name server has an error.
Try_again
A temporary error occurred on the authoritative domain name server. Please try again later.
Linux Network programming access to information via IP or host name