📘
Beej's Guide to Network Programming 正體中文版
  • 簡介
  • 原著資訊
  • 譯者誌謝
  • 進階資料
  • 簡體中文版
  • 中文授權
  • 聯絡譯者
  • 1. 導讀
    • 1.1. 本書的讀者
    • 1.2. 平台與編譯器
    • 1.3. 官方網頁與書本
    • 1.4. Solaris/SunOS 程式設計師該注意的事
    • 1.5. Windows 程式設計師該注意的事
    • 1.6. 來信原則
    • 1.7. 鏡射站台(Mirroring)
    • 1.8. 譯者該注意的
    • 1.9. 版權與散佈
  • 2. 何謂 Socket
    • 2.1 兩種 Internet Sockets
    • 2.2 底層漫談與網路理論
  • 3. IP address、結構與資料轉換
    • 3.1. IPv4 與 IPv6
      • 3.1.1. Sub network (子網段)
      • 3.1.2. Port Number(連接埠號碼)
    • 3.2. Byte Order(位元組順序)
    • 3.3. 資料結構
    • 3.4. IP 位址,續集
      • 3.4.1 Private Network
  • 4. 從 IPv4 移植為 IPv6
  • 5. System call 或 Bust
    • 5.1. getaddrinfo()-準備開始!
    • 5.2. socket()-取得 File Descriptor!
    • 5.3. bind()- 我在哪個 port?
    • 5.4. connect(),嘿!你好。
    • 5.5. listen()-有人會呼叫我嗎?
    • 5.6. accept()- 謝謝你 call 3490 port
    • 5.7. send() 與 recv()- 寶貝,我們來聊天!
    • 5.8. sendto() 與 recvfrom()- 來點 DGRAM
    • 5.9. close() 與 shutdown()- 你消失吧!
    • 5.10. getpeername()-你是誰?
    • 5.11. gethostname()-我是誰?
  • 6. Client-Server 基礎
    • 6.1. 簡單的 Stream Server
    • 6.2. 簡單的 Stream Client
    • 6.3. Datagram Sockets
  • 7. 進階技術
    • 7.1. Blocking(阻塞)
    • 7.2. select():同步 I/O 多工
    • 7.3. 不完整傳送的後續處理
    • 7.4. Serialization:如何封裝資料
    • 7.5. 資料封裝
    • 7.6. 廣播封包:Hello World!
  • 8. 常見的問題
  • 9. Man 使用手冊
    • 9.1. accept()
    • 9.2. bind()
    • 9.3. connect()
    • 9.4. close()
    • 9.5. getaddrinfo(), freeaddrinfo(), gai_strerror()
    • 9.6. gethostname()
    • 9.7. gethostbyname(), gethostbyaddr()
    • 9.8. getnameinfo()
    • 9.9. getpeername()
    • 9.10. errno
    • 9.11. fcntl()
    • 9.12. htons(), htonl(), ntohs(), ntohl()
    • 9.13. inet_ntoa(), inet_aton(), inet_addr
    • 9.14. inet_ntop(), inet_pton()
    • 9.15. listen()
    • 9.16. perror(), strerror()
    • 9.17. poll()
    • 9.18. recv(), recvfrom()
    • 9.19. select()
    • 9.20. setsockopt(), getsockopt()
    • 9.21. send(), sendto()
    • 9.22. shutdown()
    • 9.23. socket()
    • 9.24. struct sockaddr and pals
  • 10. 參考資料
    • 10.1. 書籍
    • 10.2. 網站參考資料
    • 10.3. RFC
  • 11. 原著誌謝
Powered by GitBook
On this page
  • 函式原型
  • 說明
  • 傳回值
  • 範例
  • 參考
Edit on GitHub
  1. 9. Man 使用手冊

9.7. gethostbyname(), gethostbyaddr()

取得 hostname 的 IP address for a hostname,反之亦然

函式原型

#include <sys/socket.h>
#include <netdb.h>

struct hostent *gethostbyname(const char *name); // 不建議使用!
struct hostent *gethostbyaddr(const char *addr, int len, int type);

說明

請注意:這兩個函式已經由 getaddrinfo() 與 getnameinfo() 取而代之!實際上,gethostbyname() 無法在 IPv6 中正常運作。

這些函式可以轉換 host names 與 IP addresses。例如:你可以用 gethostbyname() 取得 " 其 IP addresses,並儲存在 struct in_addr。

反之,如果你有一個 struct in_addr 或 struct in6_addr,你可以用 gethostbyaddr() 取回 hostname。gethostbyaddr() 與 IPv6 相容,但是你應該使用新的 getnameinfo() 取代之。

(如果你有一個字串是句點與數字組成的格式,你想要查詢它的 hostname,你在使用 getaddrinfo() 時最好要搭配 AI_CANONNAME flag)。

gethostbyname() 接收一個類似 "www.yahoo.com" 的字串,然後傳回一個 struct hostent,裡面包含幾萬噸的資料,包括了 IP address(其它的資訊是官方的 host name、一連串的別名、位址型別、位址長度、以及位址清單。這是個通用的資料結構,在特定的用途上也很易於使用)。

在 gethostbyaddr() 代入一個 struct in_addr 或 struct in6_addr,然後就會提供你一個相對應的 host name(如果有),因此,它是 gethostbyname() 的相反式。至於參數,addr 是一個 char*,你實際上想要用一個指向 struct in_addr 的指標傳遞;len 應是 sizeof(struct in_addr),而 type 應為 AF_INET。所以這個 struct hostent 會帶回什麼呢?它有許多欄位,包含 host 的相關資訊。

char h_name 真正的 real canonical host name。 char h_aliases 一連串的別名,可以用陣列存取—最後一個元素(element)是 NULL。 int h_addrtype address type 的答案,這個在我們的用途應該是 AF_INET。 int length address 的長度(以 byte 為單位),這個在 IP (version 4) address 是 4。 char h_addr_list 這個主機的 IP addresses 清單。雖然這是個 char**,不過實際上是 struct in_addrs 所偽裝的陣列,最後一個元素是 NULL。 h_addr 為 h_addr_list[0] 所定義的通用別名,如果你只是想要任意一個舊有的 IP addres,就用這個欄位吧。(耶,它們可以大於一個)。

傳回值

成功時傳回指向 struct hostent 結果的指標,錯誤時傳回 NULL。

跟你平常使用的錯誤報告工具不同,也不是一般的 perror(),這些函式在 h_errno 變數中有同樣的結果,可以使用 herror() 或 hstrerror() 函式印出來,這些函式運作的方式類似你常用的典型 errnor、perror() 及 strerror() 函式。

範例

// 不建議使用這個方式取得 host name
// 建議使用 getaddrinfo()!

#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
  int i;
  struct hostent *he;
  struct in_addr **addr_list;

  if (argc != 2) {
    fprintf(stderr,"usage: ghbn hostname\n");
    return 1;
  }

  if ((he = gethostbyname(argv[1])) == NULL) { // 取得 host 資訊
    herror("gethostbyname");
    return 2;
  }

  // 印出關於這個 host 的資訊:
  printf("Official name is: %s\n", he->h_name);
  printf(" IP addresses: ");
  addr_list = (struct in_addr **)he->h_addr_list;
  for(i = 0; addr_list[i] != NULL; i++) {
    printf("%s ", inet_ntoa(*addr_list[i]));
  }
  printf("\n");

  return 0;
}
// 這個方法已經被 getnameinfo() 取代了

struct hostent *he;
struct in_addr ipv4addr;
struct in6_addr ipv6addr;

inet_pton(AF_INET, "192.0.2.34", &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
printf("Host name: %s\n", he->h_name);

inet_pton(AF_INET6, "2001:db8:63b3:1::beef", &ipv6addr);
he = gethostbyaddr(&ipv6addr, sizeof ipv6addr, AF_INET6);
printf("Host name: %s\n", he->h_name);

參考

getaddrinfo(), getnameinfo(), gethostname(), errno, perror(), strerror(), struct in_addr

Previous9.6. gethostname()Next9.8. getnameinfo()

Last updated 2 years ago