📙
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. 版权与出版(Copyright and Distribution)
  • 2. 何谓 Socket
    • 2.1. 两种 Internet Sockets
    • 2.2. 底层漫谈与网路理论
  • 3. IP address丶结构与数据转换
    • 3.1. IPv4 与 IPv6
      • 31.1. Sub network (子网)
      • 3.1.2. Port Number(连接埠号码)
    • 3.2. Byte Order(字节的顺序)
    • 3.3. 数据结构
    • 3.4. IP address,Part II
      • 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()-"谢谢你调用 port 3490"
    • 5.7. send() 与 recv()- 宝贝,跟我说说话!
    • 5.8. sendto() 与 recvfrom()- 用 DGRAM 风格跟我说说话
    • 5.9. close() 与 shutdown()-从我面前消失吧!
    • 5.10. getpeername()-你是谁?
    • 5.11. gethostname()-我是誰?
  • 06-Client-Server 基础
    • 6.1. 简易的 Stream Server
    • 6.2. 简易的 Stream Client
    • 6.3. Datagram Sockets
  • 07-高等技术
    • 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()
    • close()
    • getaddrinfo(), freeaddrinfo(), gai_strerror()
    • gethostname()
    • gethostbyname(), gethostbyaddr()
    • getnameinfo()
    • getpeername()
    • errno
    • fcntl()
    • htons(), htonl(), ntohs(), ntohl()
    • inet_ntoa(), inet_aton(), inet_addr
    • inet_ntop(), inet_pton()
    • listen()
    • perror(), strerror()
    • poll()
    • recv(), recvfrom()
    • select()
    • setsockopt(), getsockopt()
    • send(), sendto()
    • shutdown()
    • socket()
    • 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 使用手册

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

Previousgethostname()Nextgetnameinfo()

Last updated 2 years ago