Network programming socket (2) - socket programming interface

  • socket common API


  • sockaddr structure

The socket API is an abstract network programming interface, suitable for various underlying network protocols, such as IPv4, IPv6, UNIX Domain Socket (inter-domain socket).

        

The address formats for IPv4 and IPv6 are defined in netinet/in.h.

An IPv4 address is represented by a sockaddr_in structure, including a 16-bit address type (identifying which type it is), a 16-bit port number and a 32-bit IP address.

As long as the first address of a sockaddr structure is obtained, the content of the structure can be determined according to the address type field without knowing which type of sockaddr structure it is.

The socket API can be represented by the struct sockaddr* type, and it needs to be converted into sockaddr_in when used. The advantage of this is the versatility of the program.


sockaddr structure:


The definition of this structure can be found in /usr/include/bits/socket.h.

sockaddr_in structure:


The definition of this structure can be found in /usr/include/netinet/in.h.

Although the interface of the socket API is sockaddr, when programming based on IPv4, the data structure used is sockaddr_in.

There are three main pieces of information in the sockaddr_in structure: address type, port number, and IP address.





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325574192&siteId=291194637