In practical network applications, often use something like "www sina. Com.cn" Such an alternative domain name server IP address to identify a function is required domain name and IP address of the actual conversion.
Linux is defined in the header file netdb.h a structure used to describe a host of related parameters, the following form:
struct hostent
{
char *h_name;//主机的正式名称
char *h_aliases;//主机的别名
int h_addrype;//主机的地址类型
int h_length;//主机的地址长度
char **h_addr_list;//主机的IP地址列表
};
#define h_addr h_addr_list[0] //主机的第一个IP地址
Domain transfer function
Linux provides conversion gethostbyname and gethostbyaddr functions for handling domain names and addresses.
#include <netdb.h>
extern int h_errno;
struct hostent *gethostbyname(const char *name);
#include <sys/socket.h>
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type);
- gethostbyname function for realizing the domain or host name to IP address string conversion parameter storage point hostname domain or host name.
- gethostbyaddr function for realizing the conversion to IP address or domain name of the host, addr parameter is a pointer to a structure containing a pointer address; parameter len is the size of this structure, in terms of a value of 4 for IPv4, for IPv6 in terms of its value 16, the parameters for the protocol family family.
- These functions are called If successful, a pointer to a pointer is returned hostent structure, if the call fails NULL pointer is returned, the global variable and set to the appropriate value h_errno.
h_errno possible values
h_errnno | Explanation |
---|---|
HOST_NOT_FOUND | Can not find corresponding host |
TRY_AGAIN | Error Retry |
NO_RECOVERY | An error has occurred irreparable |
NO_DATA | The name is valid, but did not find the record |
Applications
1. gethostbyname function
2. Function gethostbyaddr