目錄
1--域名系統(tǒng)
2--域名與 IP 地址的轉(zhuǎn)換
2-1--利用域名來獲取 IP 地址
2-2--利用 IP 地址獲取域名
3--代碼實(shí)例
3-1--gethostbyname()
3-2--gethostbyaddr()
1--域名系統(tǒng)
????????域名系統(tǒng)(Domain Name System,DNS)是對 IP 地址和域名進(jìn)行相互轉(zhuǎn)換的系統(tǒng),其核心是 DNS 服務(wù)器;
? ? ? ? 一般來說,IP 地址比較難記且經(jīng)常變化,而域名容易記且易表述,并不會輕易改變域名;(Naver 網(wǎng)站的 IP 地址是222.122.195.5 而其域名為 www.naver.com)
? ? ? ? 域名是賦予服務(wù)器端的虛擬地址,并非實(shí)際地址,因此需要通過 DNS 服務(wù)器將虛擬的域名地址轉(zhuǎn)換為實(shí)際的 IP 地址;
? ? ? ? 通常,計(jì)算機(jī)會向內(nèi)置的默認(rèn) DNS 服務(wù)器請求獲得域名對應(yīng)的 IP 地址,若默認(rèn) DNS 服務(wù)器無法解析,則默認(rèn) DNS 服務(wù)器會不斷向上級 DNS 服務(wù)器詢問,直至獲得域名對應(yīng)的 IP 地址;
程序中使用域名的必要性:
? ? ? ? ① IP 地址比域名發(fā)生變更的概率要高,因此一般不會利用 IP 地址來編寫程序;
? ? ? ? ② 域名一般不會變更,通過域名編寫程序,在程序中根據(jù)域名來獲取 IP 地址,再通過轉(zhuǎn)換的 IP 地址接入相應(yīng)的服務(wù)器,就比直接使用 IP 地址顯得更為高效;
2--域名與 IP 地址的轉(zhuǎn)換
2-1--利用域名來獲取 IP 地址
#include <netdb.h>
struct hostent* gethostbyname(const char* hostname);
// 成功時(shí)返回 hostent 結(jié)構(gòu)體地址,失敗時(shí)返回 NULL 指針
// hosttent結(jié)構(gòu)體的定義如下:
struct hostent{
char* h_name;
char** h_aliases;
int h_addrtype;
int h_length;
char** h_addr_list;
}
????????在上述 hostent 結(jié)構(gòu)體中,h_name 表示官方域名;h_aliases 表示其它域名信息(因?yàn)槎鄠€(gè)域名可以訪問同一主頁,同一個(gè) IP 也可以綁定多個(gè)域名);h_addrtype 表示地址族信息(例如 IPv4,則對應(yīng)為 AF_INET); h_length 表示 IP 地址的長度(IPv4 則為 4,IPv6 則為 16);h_addr_list 表示已整數(shù)形式保存的 IP 地址;
2-2--利用 IP 地址獲取域名
#include <netdb.h>
struct hostent* gethostbyaddr(const char* addr, socklen_t len, int family);
// 成功時(shí)返回 hostent 結(jié)構(gòu)體變量地址值,失敗時(shí)返回 NULL 指針
// addr 表示函數(shù) IP 地址信息的 in_addr 結(jié)構(gòu)體指針,為了同時(shí)傳遞 IPv4 地址之外的其他信息,該變量的類型聲明為 char 指針
// len 表示向第一個(gè)參數(shù)傳遞的地址信息的字節(jié)數(shù),IPv4 為 4,IPv6 為 16
// family 表示傳遞的地址族信息,IPv4 為 AF_INET,IPv6 為 AF_INET6
3--代碼實(shí)例
3-1--gethostbyname()
// gcc gethostbyname.c -o gethostbyname
// ./gethostbyname www.baidu.com
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
void error_handling(char *message){
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
int main(int argc, char *argv[]){
int i;
struct hostent *host;
if(argc != 2){
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
host = gethostbyname(argv[1]);
if(!host){
error_handling("gethost... error");
}
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;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-700113.html
3-2--gethostbyaddr()
// gcc gethostbyaddr.c -o gethostbyaddr
// ./gethostbyaddr 199.59.148.206
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
void error_handling(char *message){
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
int main(int argc, char *argv[]){
int i;
struct hostent *host;
struct sockaddr_in addr;
if(argc != 2){
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
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");
}
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;
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-700113.html
到了這里,關(guān)于《TCP/IP網(wǎng)絡(luò)編程》閱讀筆記--域名及網(wǎng)絡(luò)地址的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!