- Three Linux socket addresses
- Common socket address of sockaddr
typedef unsigned short sa_family_t;struct sockaddr { sa_family_t sa_family; /* address family, AF_xxx */ char sa_data[14]; /* 14 bytes of protocol address */}
- Socket address of sockaddr_in Internet
/* Structure describing an internet socket address. */struct sockaddr_in {_ sockaddr_common (SIN _); in_port_t sin_port;/* port number. */struct in_addr sin_addr;/* Internet address. * // * pad to size of 'struct sockadd '. */unsigned char sin_zero [sizeof (struct sockaddr)-_ sockaddr_common_size-sizeof (in_port_t)-sizeof (struct in_addr)]; /* The character array sin_zero [8] exists to ensure that the size of the struct sockaddr_in is equal to the size of the struct sockaddr */}; struct in_addr {unit32_t s_addr; /* Internet address */};
- Socket address of the sockaddr_un UNIX System
Struct sockaddr_un {sa_family_t sun_family;/* pf_unix or af_unix */Char sun_path [unix_path_max];/* path */};
- Address Translation in Linux socket
In general, the X86 architecture PC uses the small-end mode to store data, while in the socket, the large-end mode is required, so the relevant conversion is required. Common conversion functions include:
- Socket universal conversion functions
Ntohl converts 32-bit network data into host data
Ntohs converts 16-bit network data into host data
Htonl converts 32-bit host data into network data
Htons converts 16-bit network data into network data
Inet_ntoa converts the IP address of the network byte to a decimal vertex address.
Inet_aton converts the dotted-decimal address to the IP address of the network byte.
Inet_network converts the dot-decimal to 32-bit data of host bytes.
Generally, the sockaddr_in structure is used when network programming is used to fill in the value. When passed in as a function parameter, it is converted to the sockaddr structure, which is a string of 16 characters.Long.