代码改变世界

结构体sockadrr、sockaddr_in、in_addr的定义

2017-08-08 19:14  ljtcnblogs  阅读(994)  评论(0编辑  收藏  举报

/* Internet address.  */
typedef uint32_t in_addr_t;
struct in_addr
  {
    in_addr_t s_addr;
  };
 
typedef unsigned short int sa_family_t;
#define __SOCKADDR_COMMON(sa_prefix) \
   sa_family_t sa_prefix##family
 
/* Structure describing an Internet socket address.  */
struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);
    in_port_t           sin_port;                  /* Port number.  */
    struct in_addr   sin_addr;                 /* Internet address.  */
    unsigned char  sin_zero[sizeof (struct sockaddr) - __SOCKADDR_COMMON_SIZE - sizeof (in_port_t) - sizeof (struct in_addr)];
                                                             /* Pad to size of `struct sockaddr'.  */
  };
 
typedef unsigned short    sa_family_t;
/*1003.1g requires sa_family_t and that sa_data is char. */
struct sockaddr {
    sa_family_t    sa_family;        /* address family, AF_xxx    */
    char               sa_data[14];    /* 14 bytes of protocol address    */
};

 

struct sockaddr是通用的socket地址,struct sockaddr_in是Internet socket,二者可以进行类型转换