网络编程中4个重要的地址数据结构sockaddr_in sockaddr_in6 sockaddr sockaddr_storage


1. IPv4: struct  sockaddr_in (internet), 16个字节

复制代码
 1 struct sockaddr_in {
 2      sa_family_t sin_family;             /* AF_INET */
 3      in_port_t sin_port;                 /* Port number.  */
 4      struct in_addr sin_addr;            /* Internet address.  */
 5 
 6      /* Pad to size of `struct sockaddr'.  */
 7      unsigned char sin_zero[sizeof (struct sockaddr) -
 8                             sizeof (sa_family_t) -
 9                             sizeof (in_port_t) -
10                             sizeof (struct in_addr)];
11 };
12 typedef uint32_t in_addr_t;
13 struct in_addr  {
14     in_addr_t s_addr;                    /* IPv4 address */
15 };
复制代码

2. IPv6: struct sockaddr_in6, 28个字节

复制代码
 1 struct sockaddr_in6 {
 2     sa_family_t sin6_family;    /* AF_INET6 */
 3     in_port_t sin6_port;        /* Transport layer port # */
 4     uint32_t sin6_flowinfo;     /* IPv6 flow information */
 5     struct in6_addr sin6_addr;  /* IPv6 address */
 6     uint32_t sin6_scope_id;     /* IPv6 scope-id */
 7 };
 8 struct in6_addr {
 9     union {
10         uint8_t u6_addr8[16];
11         uint16_t u6_addr16[8];
12         uint32_t u6_addr32[4];
13     } in6_u;
14 
15     #define s6_addr                 in6_u.u6_addr8
16     #define s6_addr16               in6_u.u6_addr16
17     #define s6_addr32               in6_u.u6_addr32
18 };
复制代码

3. 通用结构体1: struct sockaddr(local), 16个字节

1 struct sockaddr { 
2      sa_family_t sa_family;       /* Address family */
3      char sa_data[14];            /* protocol-specific address */
4 };

4. 通用结构体2: struct sockaddr_storage(ss),128个字节

 

复制代码
 1 /* Structure large enough to hold any socket address 
 2 (with the historical exception of AF_UNIX). 128 bytes reserved.  */
 3 
 4 #if ULONG_MAX > 0xffffffff
 5 # define __ss_aligntype __uint64_t
 6 #else
 7 # define __ss_aligntype __uint32_t
 8 #endif
 9 #define _SS_SIZE        128
10 #define _SS_PADSIZE     (_SS_SIZE - (2 * sizeof (__ss_aligntype)))
11 
12 struct sockaddr_storage
13 {
14     sa_family_t ss_family;      /* Address family */
15     __ss_aligntype __ss_align;  /* Force desired alignment.  */
16     char __ss_padding[_SS_PADSIZE];
17 };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值