1, socket interface address structure
The POSIX specification requires only three members in a struct: sin_family, sin_addr, Sin_port. Where sin_addr is also a structure
2, universal socket interface address structure
A set of interface functions is defined as a pointer to a generic set of interface address structures. Since the definition of the SET interface function does not use the void* pointer type,
Therefore, you must force type conversions when you call the socket interface function.
Reason for using the generic socket address structure: The kernel must check the value of the sin_family to determine the type of the structure, based on the common socket interface address knot
3. Byte sorting function
There are two ways in which multibyte data is stored in memory. The small-endian byte sequence stores the low bytes at the start address, and the high-end byte-order byte is stored at the start address.
The POSIX specification stipulates that some members of the socket address structure are maintained by the network byte order, so it is important to note that the host byte order and network byte order
The conversion.
#include <netinet/in.h>
Returns the network byte order value
uint16_t htons (uint16_t)
uint32_t htonl (uint32_t)
Return host byte order value
uint16_t Ntohs (uint16_t)
uint32_t Ntohl (uint32_t)
4. Address conversion function
Address translation functions are used to convert addresses between ASCII strings and binary values of network byte order
#include <arpa/inet.h>
int Inet_aton (const char *strptr, struct in_addr *addrptr)
Converts the string referred to by StrPtr to a 32-bit network byte-order binary value, saving the result in addrptr. Successful return 1, failure returns 0.
in_addr_t inet_addr (const char *strptr)
Returns a 32-bit network byte-order binary value. A constant value of Inaddr_none (255.255.255.255) is returned on failure, so the function cannot handle
Dotted decimal number string 255.255.255.255
Char *inet_ntoa (struct in_addr inaddr)
Converts a 32-bit network byte-order binary value into a dotted decimal number string.
The following two conversion functions can be processed for both IPv4 and IPv6 addresses.
#include <arpa/inet.h>
Dotted decimal to binary conversion
int Inet_pton (int family, const char *strptr, void *addrptr)
Binary point-to-decimal conversion
const char *inet_ntop (int family, const void *addrptr, char *strptr, size_t len)
Introduction to Programming of socket interface