gethostbyname & getaddrinfo

本文详细探讨了在Unix/Linux系统中用于地址解析的gethostbyname和getaddrinfo函数。从它们的基本使用到功能差异,再到在IPv4和IPv6环境下的应用,逐一剖析。同时,阐述了在向IPv6过渡过程中,如何从gethostbyname过渡到更现代且兼容性的getaddrinfo函数,以及这两个函数在错误处理和性能上的考量。
摘要由CSDN通过智能技术生成
struct sockaddr {
     
     sa_family_t sin_family;//地址族
   char sa_data[14]; //14字节,包含套接字中的目标地址和端口信息               
}; 
struct sockaddr_in {
   
    sa_family_t     sin_family;
    uint16_t        sin_port; //2字节
    struct in_addr  sin_addr; //4字节
    char            sin_zero[8];
};
struct in_addr {
   
    In_addr_t s_addr;//32位IP地址
};
/*

 The hostent structure is defined in <netdb.h> as follows:
        
           struct hostent {
               char  *h_name;            /* official name of host */
    //           char **h_aliases;         /* alias list */
    //          int    h_addrtype;        /* host address type */
    //           int    h_length;          /* length of address */
    //           char **h_addr_list;       /* list of addresses */
    //       }
    //       #define h_addr h_addr_list[0] /* for backward compatibility */
        /*
       The members of the hostent structure are:

       h_name The official name of the host.

       h_aliases
              An array of alternative names for the host,  terminated  by  a  NULL
              pointer.

       h_addrtype
              The type of address; always AF_INET or AF_INET6 at present.

       h_length
              The length of the address in bytes.

       h_addr_list
              An  array  of pointers to network addresses for the host (in network
              byte order), terminated by a NULL pointer.

       h_addr The first address in h_addr_list for backward compatibility.
    
    //struct hostent *gethostbyname(const char *name);

*/

#include <stdio.h>
<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值