socket的常用数据结构


IPv4 Socket Address Structure

netinet/in.h

struct in_addr {
  in_addr_t   s_addr;           /* 32-bit IPv4 address normally uint32_t*/
                                /* network byte ordered */
};

struct sockaddr_in {
  [uint8_t         sin_len;]      /* length of structure (16) */
  sa_family_t     sin_family;   /* AF_INET if sin_len exit uint8_t,else unsigned short 16 bit integer*/
  in_port_t       sin_port;     /* 16-bit TCP or UDP port number normally uint_16*/
                                /* network byte ordered */
  struct in_addr  sin_addr;     /* 32-bit IPv4 address */
                                /* network byte ordered */
  char            sin_zero[8];  /* unused */
};//all socket address structures are at least 16 bytes in size

Generic Socket Address Structure

sys/socket.h

struct sockaddr {
  [uint8_t      sa_len;]     /*similar as sockaddr_in*/
  sa_family_t  sa_family;    /* address family: AF_xxx value */
  char         sa_data[14];  /* protocol-specific address */
};

IPv6 Socket Address Structure

netinet/in.h

struct in6_addr {
  uint8_t  s6_addr[16];          /* 128-bit IPv6 address */
                                 /* network byte ordered */
};

#define SIN6_LEN      /* required for compile-time tests */

struct sockaddr_in6 {
  [uint8_t         sin6_len;]      /* length of this struct (28) controled by SIN6_LEN macro*/
  sa_family_t     sin6_family;   /* AF_INET6 */
  in_port_t       sin6_port;     /* transport layer port# */
                                 /* network byte ordered */
  uint32_t        sin6_flowinfo; /* flow information, undefined */
  struct in6_addr sin6_addr;     /* IPv6 address */
                                 /* network byte ordered */
  uint32_t        sin6_scope_id; /* set of interfaces for a scope */
};

New Generic Socket Address Structure

netinet/in.h

struct sockaddr_storage {
  uint8_t      ss_len;       /* length of this struct (implementation dependent) */
  sa_family_t  ss_family;    /* address family: AF_xxx value */
  /* implementation-dependent elements to provide:
   * a) alignment sufficient to fulfill the alignment requirements of
   *    all socket address types that the system supports.
   * b) enough storage to hold any type of socket address that the
   *    system supports.
   */
};

连接过程

连接过程

socket Function

#include <sys/socket.h>

int socket (int family, int type, int protocol);

/* Returns: non-negative descriptor if OK, -1 on error */

unix网络编程 http基础


Copyright © FengGuangtu 2017