这里实现域名<— —> 服务器的双向访问
- gethostbyname:已知域名,查看 IP 地址
- gethostbyaddr:通过 IP 地址,查看其对应的域名。
gethostbyname 利用域名获取 IP 地址
#include <winsock2.h>struct hostent* gethostbyname(const char *name);// 成功时返回 hostent 结构体变量地址值// 失败时返回 NULL 指针
这个函数使用的时候很方便,只要传递域名字符串,就会返回域名对应的 IP 地址。只是返回时,地址信息装入 hostent 结构体。此结构体定义如下。
struct hostent {char *h_name; // official namechar **h_ailases; // alias listint h_addrtype; // host address typeint h_length; // address lengthchar **h_addr_list; // address list}
可以看到,通过调用 gethostbyname 函数返回的 hostent 结构体指针,其内容里不光有 IP 信息,还有其他信息。下面简要说明上述结构体各个成员。
h_name:该变量中有官方域名,官方域名代表某一主页,但实际上,一些著名公司的域名并未用官方域名注册。h_aliases:可以通过多个域名访问同一主页。同一 IP 可以绑定多个域名,因此,除官方域名外还可指定其他域名。这些信息可以通过h_aliases获得。h_addrtype:gethostbyname函数不仅支持 IPv4,还支持 IPv6。因此,可以通过此变量获取保存在 h_addr_list 的 IP 地址的地址族信息。若是 IPv4,则此变量存有 AF_INET。h_length:保存 IP 地址长度。若是 IPv4 地址,因为是 4 个字节,则保存 4; IPv6时,因为是 16 个字节,故保存 16.h_addr_list:这时最重要的成员。通过此变量以整数形式保存域名对应的 IP 地址。另外,用户较多的网站有可能分配多个 IP 给同一域名,利用多个服务器进行负载均衡。此时,同样可以通过此变量来获取 IP 地址信息。
下面是通过例子演示 gethostbyname 函数的应用,基于 Linux 平台。
// gethostbyname.c 基于 Linux 平台#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <netdb.h>void error_handling(char *message);int main(int argc, char *argv[]) {// 1.检查输入if (argc!=2) {printf("Usage: %s <IP>\n", argv[0]);exit(1);}struct sockaddr_in addr;struct hostent *host;memset(&addr, 0, sizeof(addr));addr.sin_addr.s_addr = inet_addr(argv[1]);host = gethostbyaddr( (char*)&addr.sin_addr, 4, AF_INET);if (!host)error_handling("gethost ... error");int i;printf("Official name: %s \n", host->h_name);for (i=0; host->h_aliases[i]; i++)printf("Aliases %d: %s \n", i+1, host->h_aliases[i]);printf("Address type: %s \n", (host->h_addrtype==AF_INET) ? "AF_INET" : "AF_INET6");for (i=0; host->h_addr_list[i]; i++)printf("IP addr %d: %s \n", i+1, inet_ntoa(*(struct in_addr*) host->h_addr_list[i]));return 0;}void error_handling(char *message) {fputs(message, stderr);fputc('\n', stderr);exit(1);}
编译运行,这里我获取的是我的域名 hamsercoder.top 对应的 IP 地址:

gethostbyaddr 利用 IP 地址获取域名
在直到了 IP 地址的情况,可以利用 gethostbyaddr 获取域名相关信息。
struct hostent* gethostbyaddr(const char *addr, socklen_t len, int family);// 成功时返回 hostent 结构体变量地址值,失败时返回 NULL 指针// 参数:// -addr:含有 IP 地址信息的 in_addr 结构体指针,为了同时传递 IPv4 地址之外的其他信息,该// 变量的类型声明为 char 指针。// -len: 向第一个参数传递的地址信息,IPv4 时为 4,IPv6 时为 16.// -family:传递地址族信息,IPv4 时为 AF_INET,IPv6 时为 AF_INET6
下面通过示例演示该函数的使用方法,平台实现为 Linux。
// gethostbyaddr.c 基于 Linux 平台#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <netdb.h>void error_handling(char *message);int main(int argc, char *argv[]) {// 1.检查输入if (argc!=2) {printf("Usage: %s <IP>\n", argv[0]);exit(1);}struct sockaddr_in addr;struct hostent *host;memset(&addr, 0, sizeof(addr));addr.sin_addr.s_addr = inet_addr(argv[1]);host = gethostbyaddr( (char*)&addr.sin_addr, 4, AF_INET);if (!host)error_handling("gethost ... error");int i;printf("Official name: %s \n", host->h_name);for (i=0; host->h_aliases[i]; i++)printf("Aliases %d: %s \n", i+1, host->h_aliases[i]);printf("Address type: %s \n", (host->h_addrtype==AF_INET) ? "AF_INET" : "AF_INET6");for (i=0; host->h_addr_list[i]; i++)printf("IP addr %d: %s \n", i+1, inet_ntoa(*(struct in_addr*) host->h_addr_list[i]));return 0;}void error_handling(char *message) {fputs(message, stderr);fputc('\n', stderr);exit(1);}
通过 ping 得到 www.google.com 的一个主机,然后运行程序

Windows 平台下的实现
gethostbyname 实现
// gethostbyname_win.c 基于 Windows 平台#include <stdio.h>#include <stdlib.h>#include <winsock2.h>void ErrorHandling(char *message);int main(int argc, char *argv[]) {// 1.检查输入if (argc!=2) {printf("Usage: %s <IP>\n", argv[0]);exit(1);}WSADATA wsaData;if ( WSAStartup(MAKEWORD(2,2), &wsaData) != 0)ErrorHandling("WSAStartup() error");// 2.通过域名获取地址struct hostent *host;host = gethostbyname( (argv[1]) );if (!host)ErrorHandling("gethost... error");int i;printf("Official name: %s \n", host->h_name);for (i=0; host->h_aliases[i]; i++)printf("Aliases %d: %s \n", i+1, host->h_aliases[i]);printf("Address type: %s \n", (host->h_addrtype==AF_INET) ? "AF_INET" : "AF_INET6");for (i=0; host->h_addr_list[i]; i++)printf("IP addr %d: %s \n", i+1, inet_ntoa(*(struct in_addr*) host->h_addr_list[i]));WSACleanup();return 0;}void ErrorHandling(char *message) {fputs(message, stderr);fputc('\n', stderr);exit(1);}
结果如下,可以看到和 Linux 的结果差了一些,这个是因为 hamstercoder.top 就是部署在 Linux 下的,所以结果上会有差异。

gethostbyaddr 实现
和 Linux 下的类似,函数如下
// gethostbyaddr_win.c 基于 Windows 平台#include <stdio.h>#include <stdlib.h>#include <winsock2.h>void ErrorHandling(char *message);int main(int argc, char *argv[]) {// 1.检查输入if (argc!=2) {printf("Usage: %s <IP>\n", argv[0]);exit(1);}WSADATA wsaData;if ( WSAStartup(MAKEWORD(2,2), &wsaData) != 0)ErrorHandling("WSAStartup() error");// 2.通过域名获取地址struct hostent *host;SOCKADDR_IN addr;memset(&addr, 0, sizeof(addr));addr.sin_addr.s_addr = inet_addr(argv[1]);host = gethostbyaddr( (char*)&addr.sin_addr, 4, AF_INET );if (!host)ErrorHandling("gethost... error");int i;printf("Official name: %s \n", host->h_name);for (i=0; host->h_aliases[i]; i++)printf("Aliases %d: %s \n", i+1, host->h_aliases[i]);printf("Address type: %s \n", (host->h_addrtype==AF_INET) ? "AF_INET" : "AF_INET6");for (i=0; host->h_addr_list[i]; i++)printf("IP addr %d: %s \n", i+1, inet_ntoa(*(struct in_addr*) host->h_addr_list[i]));WSACleanup();return 0;}void ErrorHandling(char *message) {fputs(message, stderr);fputc('\n', stderr);exit(1);}
这里同样查看 199.59.148.201 的域名,结果和 Linux 下的一样。

