Network programming - socket definition and address format

Network programming - socket definition and address format


Table of contents

  1. What is socket?
  2. Socket address format

1. What is a socket?

insert image description here

  1. In network programming, socket is translated as socket or socket interface, which means that the network connection and data transmission and reception can be quickly completed through the socket access method.
  2. The figure above shows the core logic of client and server work in network programming.
  3. On the server side, before the client initiates a connection request, the server side must be initialized.
    1. Initialize the socket.
    2. Execute the bind function to bind the service capability to a known address and port.
    3. Then execute the listen operation to convert the original socket into a socket on the server side.
    4. The server finally blocks on accept and waits for the client request.
  4. When the server is ready, the client needs to initialize the socket first, and then execute connect to initiate a connection request to the address and port of the server, where the address and port must be known by the client in advance. The connect request is the TCP three-way handshake (Three-way Handshake).
  5. After the three-way handshake is completed, the client and server establish a connection and enter the data transmission process.
    1. The client process initiates a write byte stream operation to the operating system kernel, the kernel protocol stack transmits the byte stream to the server through the network device, the server gets information from the kernel, reads the byte stream from the kernel into the process, and Start the processing of business logic, and after completion, the server will write the obtained results to the client in the same way.
    2. So once the connection is established, the transmission of data is no longer one-way, but two-way .
  6. When the client completes the interaction with the server, such as performing a Telnet operation, or an HTTP request, and needs to disconnect from the server, the close function will be executed.
    1. At this time, the operating system kernel will send a FIN packet to the server through the original connection link, and the server will perform a passive shutdown after receiving it. At this time, the entire link is in a half-closed state.
    2. After that, the server will also execute the close function, and the entire link will be truly closed.
    3. In the semi-closed state, the party that initiates the close request considers the connection to be normal until it receives the FIN packet from the other party.
    4. In the fully closed state, both parties perceive that the connection has been closed.
  7. All the above operations are done through socket. Whether it is the client's connect, or the server's accept, or read/write operations, etc., socket is the only way to establish a connection and transmit data .

2. Socket address format

  1. When using sockets, the addressing problem of both communication parties must be solved first. The address of the socket is required to establish a connection. The format of the address of the socket is as follows.

1. Common socket address format

  1. Common address structure for sockets :
/* POSIX.1g 规范规定了地址族为 2 字节的值.  */
typedef unsigned short int sa_family_t;
/* 描述通用套接字地址  */
struct sockaddr{
    
    
    sa_family_t sa_family;  /* 地址族.  16-bit*/
    char sa_data[14];   /* 具体的地址值 112-bit */
}; 
  1. The first field is the address family, which indicates how to interpret and save the address. There are many definitions of address families in glibc, the commonly used ones are as follows:
    • AF_LOCAL: Indicates the local address, corresponding to the Unix socket, generally used for local socket communication, and can also be written as AF_UNIX, AF_FILE.
    • AF_INET: IPv4 address used by the Internet.
    • AF_INET6: IPv6 address used by the Internet.
  2. AF_ means Address Family, and PF_ means Protocol Family protocol family, such as PF_INET, PF_INET6, etc. Use the value of AF_xxx to initialize the socket address, and use the value of PF_xxx to initialize the socket. As you can see in the <sys/socket.h> header file, these two values ​​are in one-to-one correspondence.
/* 各种地址族的宏定义  */
#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
  1. sockaddr is a general address structure applicable to many address families.

2. IPv4 socket format address

  1. Commonly used IPv4 address family structure:
//IPV4 套接字地址,32bit 值.
typedef uint32_t in_addr_t; 
struct in_addr
  {
    
    
    in_addr_t s_addr;
  };
  
struct sockaddr_in //描述 IPV4 的套接字地址格式
  {
    
    
    sa_family_t sin_family; 	//16-bit */
    in_port_t sin_port;     	//端口口  16-bit
    struct in_addr sin_addr;    //Internet address. 32-bit
 
    unsigned char sin_zero[8]; 	//这里仅仅用作占位符,不做实际用处
  };
  1. Same as sockaddr, there is a 16-bit sin_family field, the value is AF_INET for IPv4.
  2. Port number, the port number is up to 16-bit, that is, the maximum supported 2 to the 16th power (65535).
    1. Reserved ports are established by convention, and have been used by corresponding services, such as port 21 of ftp, port 22 of ssh, port 80 of http, etc.
    2. Generally, ports larger than 5000 can be used as ports of your own application.
  3. The following are reserved ports defined by glibc.
/* Standard well-known ports.  */
enum
  {
    
    
    IPPORT_ECHO = 7,    /* Echo service.  */
    IPPORT_DISCARD = 9,   /* Discard transmissions service.  */
    IPPORT_SYSTAT = 11,   /* System status service.  */
    IPPORT_DAYTIME = 13,  /* Time of day service.  */
    IPPORT_NETSTAT = 15,  /* Network status service.  */
    IPPORT_FTP = 21,    /* File Transfer Protocol.  */
    IPPORT_TELNET = 23,   /* Telnet protocol.  */
    IPPORT_SMTP = 25,   /* Simple Mail Transfer Protocol.  */
    IPPORT_TIMESERVER = 37, /* Timeserver service.  */
    IPPORT_NAMESERVER = 42, /* Domain Name Service.  */
    IPPORT_WHOIS = 43,    /* Internet Whois service.  */
    IPPORT_MTP = 57,
 
 
 
 
    IPPORT_TFTP = 69,   /* Trivial File Transfer Protocol.  */
    IPPORT_RJE = 77,
    IPPORT_FINGER = 79,   /* Finger service.  */
    IPPORT_TTYLINK = 87,
    IPPORT_SUPDUP = 95,   /* SUPDUP protocol.  */
 
 
    IPPORT_EXECSERVER = 512,  /* execd service.  */
    IPPORT_LOGINSERVER = 513, /* rlogind service.  */
    IPPORT_CMDSERVER = 514,
    IPPORT_EFSSERVER = 520,
 
 
    /* UDP ports.  */
    IPPORT_BIFFUDP = 512,
    IPPORT_WHOSERVER = 513,
    IPPORT_ROUTESERVER = 520,
 
 
    /* Ports less than this value are reserved for privileged processes.  */
    IPPORT_RESERVED = 1024,
 
 
    /* Ports greater this value are reserved for (non-privileged) servers.  */
    IPPORT_USERRESERVED = 5000
  1. An IPv4 address is a 32-bit field, and the maximum number of supported addresses is 2 to the 32nd power, which is about 4.2 billion.

3. IPv6 socket address format

  1. The address structure of IPv6:
struct sockaddr_in6
  {
    
    
    sa_family_t sin6_family; 	//16-bit
    in_port_t sin6_port;  		//传输端口号 # 16-bit 
    uint32_t sin6_flowinfo; 	//IPv6 流控信息 32-bit
    struct in6_addr sin6_addr;  //IPv6 地址 128-bit
    uint32_t sin6_scope_id; 	//IPv6 域 ID 32-bit
  };
  1. The length of the entire structure is 28 bytes, of which flow control information and domain IP do not appear on the official website of glibc, and the other is a currently unused field. The address family is AF_INET6, the port is the same as the IPv4 address, and the key address is upgraded from 32 bits to 128 bits, which completely solves the problem of insufficient addressing numbers.
  2. Both IPv4 and IPv6 address formats are in the format of Internet sockets, and there is also a local socket format used for communication between local processes, namely AF_LOCAL.
struct sockaddr_un {
    
    
    unsigned short sun_family; 	//固定为 AF_LOCAL
    char sun_path[108];   		//路径名
};

4. Comparison of several socket address formats

  1. The comparison of several addresses is shown in the figure below. The length of the IPv4 and IPv6 socket address structures is fixed, while the length of the local address structure is variable.

image.png

Guess you like

Origin blog.csdn.net/weixin_41910694/article/details/127977945