Socket and address

socket

In a typical client / server scenario, the application uses socket to communicate as follows:

  • Each application creates a socket. Socket is a "device" that allows communication, which is used by both applications

  • The server binds its socket to a well-known address so that the client can locate its location

Socket address format

When using sockets, we should first solve the problem of addressing both sides of communication. We need the address of the socket to establish a connection.

Universal socket address format

sockaddr is a general address structure, which means it is suitable for a variety of address families. The only purpose of this type is to convert various specific address structures into a single type for socket system calls.

/* POSIX.1g The specification specifies that the address family is a 2 - byte value  */
typedef unsigned short int sa_family_t;
/* Describes the universal socket address  */
struct sockaddr{
    sa_family_t sa_family;  /* Address family 16-bit*/
    char sa_data[14];   /* The specific address value is 112 bit */
  }; 

sa_family is an address family, which indicates how to interpret and save addresses:

  • AF_LOCAL: refers to the local address and corresponds to the Unix socket. This case is generally used for local socket communication, and can also be written as AF in many cases_ Unix,AF_FILE

  • AF_INET: IPv4 address used by the Internet

  • AF_INET6: IPv6 address used by the Internet

AF_ It means Address Family, but in many cases, we will also see PF_ Represents a macro, which means Protocol Family, that is, Protocol Family< sys/socket. h> It can be clearly seen in the header file that the two values themselves correspond to each other one by one:

/* Macro definition of various address families  */
#define AF_UNSPEC PF_UNSPEC
#define AF_LOCAL  PF_LOCAL
#define AF_UNIX   PF_UNIX
#define AF_FILE   PF_FILE
#define AF_INET   PF_INET
#define AF_AX25   PF_AX25
#define AF_IPX    PF_IPX
#define AF_APPLETALK  PF_APPLETALK
#define AF_NETROM PF_NETROM
#define AF_BRIDGE PF_BRIDGE
#define AF_ATMPVC PF_ATMPVC
#define AF_X25    PF_X25
#define AF_INET6  PF_INET6

IPv4 socket format address

/* IPV4 Socket address, 32 bit value  */
typedef uint32_t in_addr_t;
struct in_addr
  {
    in_addr_t s_addr;
  };
  
/* Describes the socket address format of IPV4  */
struct sockaddr_in
  {
    sa_family_t sin_family; /* 16-bit */
    in_port_t sin_port;     /* Port 16 bit*/
    struct in_addr sin_addr;    /* Internet address. 32-bit */
​
​
    /* This is only used as a placeholder, not for practical use  */
    unsigned char sin_zero[8];
  };

IPv4 address is a 32-bit field. It can be imagined that the maximum number of addresses supported is the 32nd power of 2, about 4.2 billion.

The maximum port number is 16 bit, that is, it supports the 16th power of 2, so we should know that the maximum port number supporting addressing is 65535.

IPv6 socket address format

struct sockaddr_in6
{
    sa_family_t sin6_family; /* 16-bit */
    in_port_t sin6_port;  /* Transmission port number # 16 bit */
    uint32_t sin6_flowinfo; /* IPv6 Flow control information 32-bit*/
    struct in6_addr sin6_addr;  /* IPv6 Address 128 bit */
    uint32_t sin6_scope_id; /* IPv6 Domain ID 32 bit */
};

Local socket format

The above address formats of both IPv4 and IPv6 are Internet socket format, and there is also a local socket format, that is, AF mentioned earlier_ LOCAL.

struct sockaddr_un {
    unsigned short sun_family; /* Fixed to AF_LOCAL */
    char sun_path[108];   /* Pathname */
};

Comparison of several socket address formats

The length of IPv4 and IPv6 socket address structure is fixed, while the length of local address structure is variable:

 

Keywords: network socket

Added by Allenport on Mon, 20 Dec 2021 13:57:50 +0200