問題:gethostname, gethostbyname 這2個名字相似的網絡編程API,有何聯系與區別?
gethostname 獲取主機名稱(非登錄用戶名),其參數用於存儲查詢結果。gethostname 查找的是/etc/hostname
文件,得到本主機名。其獲得的主機名,可用於getaddrinfo的主機名參數,獲取本地主機IP地址。
gethostbyname 獲取網絡主機項(entry),其參數是主機名。執行的是對DNS系統A記錄的查詢(通過本地主機名查DNS IP地址信息,優先查找的是本地/etc/hosts
文件), 只能返回IPv4地址。不可重入,其可重入版本是gethostbyname_r。
下面對這個系列的函數,進行更詳細介紹:
gethostname 系列函數
gethostname 獲取主機名稱。
sethostname,用於設置主機名稱。
#include <unistd.h>
int gethostname(char *name, size_t len);
int sethostname(const char *name, size_t len);
gethostname得到的host name,等價於$ uname -n
命令。
/etc/hostname內容:
ubuntu
$ uname -n
命令運行結果
$ uname -n
ubuntu
gethostbyname 系列函數
gethostbyname(), gethostbyaddr(), herror(), hstrerror() 函數已廢棄。應用程序應該用getaddrinfo(), getnaminfo(), gai_strerror替代。
gethostbyname*()得到查詢主機主機名稱name的hostent。hostent 是host entry簡寫,該結構記錄主機的信息,包括主機名、別名、地址類型、地址長度和地址列表。之所以主機的地址是一個列表的形式,原因是當一個主機有多個網絡接口時,會有多個地址。
gethostname通常用於通過host name得到地址信息。
#include <netdb.h>
struct hostent *gethostbyname(const char *name);
/* GNU extensions */
int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
示例
gethostname獲取本地host name,gethostbyname通過host name得到本地ip地址信息。
char name[100];
if (gethostname(name, sizeof(name)) < 0) {
perror("gethostname error");
exit(1);
}
printf("host name = %s\n", name);
struct hostent *hp;
if ((hp = gethostbyname(name)) == NULL) {
perror("gethostbyname error");
exit(1);
}
int j = 0;
while (hp->h_addr_list[j] != NULL) {
printf("Official name = %s\n", hp->h_name);
printf("Alias name = %s\n", hp->h_aliases[j]);
printf("addr type = %d\n", hp->h_addrtype); /* AF_INET: 2; AF_INET6: 10 */
printf("ip len = %d bytes\n", hp->h_length); /* IPv4: 4bytes; IPv6: 16bytes */
printf("ip = %s\n", inet_ntoa(*(struct in_addr *)hp->h_addr_list[j]));
j++;
}
運行結果
host name = ubuntu
Official name = ubuntu
Alias name = (null)
addr type = 2
ip len = 4 bytes
ip = 127.0.1.1
本地/etc/hosts文件內容如下。可以看到gethostbyname通過本地主機名ubuntu,得到的ip地址與/etc/hosts文件內容一致。
127.0.0.1 localhost
127.0.1.1 ubuntu