网络信息检索函数

man gethostbyname
1 #include <netdb.h>
2 extern int h_errno; //错误号
3
4 //name一般为域名,通过域名获取主机相关信息
5 struct hostent *gethostbyname(const char *name);
6
7 #include <sys/socket.h> /* for AF_INET */
8 struct hostent *gethostbyaddr(const void *addr,
9 socklen_t len, int type);
10
11 void sethostent(int stayopen);
12
13 void endhostent(void); //释放hostent结构体的变量值
14
15 void herror(const char *s); //打印出错信息
16
17 const char *hstrerror(int err); //打印出错信息
18
19 /* System V/POSIX extension */
20 struct hostent *gethostent(void);
21 //返回值:成功返回结构体指针hostent, 错误返回一个空指针。
22
23 The hostent structure is defined in <netdb.h> as follows:
24
25 struct hostent {
26 char *h_name; /* official name of host */
27 char **h_aliases; /* alias list */
28 int h_addrtype; /* host address type */
29 int h_length; /* length of address */
30 char **h_addr_list; /* list of addresses */
31 }
32 #define h_addr h_addr_list[0] /* for backward compatibility */
33 //指向主机多个网络地址(NBD,32位整数)的指针数组
说明:
IPV4中使用gethostbyname()函数完成主机名到地址解析,此函数仅支持IPV4,且不允许调用者指定所需地址类型的任何信息,返回的结构只包含了用于存储IPV4地址的空间。IPV6中引入了getaddrinfo()的新API,既可用于IPV4也可用于IPV6
来源:https://www.cnblogs.com/y4247464/p/12237870.html