In the Linux environment, the structure struct sockaddr is defined in/usr/include/linux/socket.h, as follows:
typedef unsigned short sa_family_t;
struct SOCKADDR {
sa_family_t sa_family; /* Address family, AF_XXX * *
Char sa_data[14]; * Bytes of protocol address * *
};
In the Linux environment, the structure struct sockaddr_in is defined in/usr/include/netinet/in.h, as follows:
/* Structure describing an Internet sockets 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 sockaddr '. */
unsigned char sin_zero[sizeof (struct sockaddr)-
__sockaddr_common_size-
sizeof (in_port_t)-
sizeof (struct in_addr)];
/* character Array sin_zero[8] exists to ensure that the size of the structure struct sockaddr_in is equal to the size of the struct sockaddr of the structure body.
};
struct sockaddr is a generic socket address, and struct sockaddr_in is the address form of the socket in the Internet environment, which is 16 bytes in length. The two are parallel structures, and pointers to sockaddr_in structures can point to sockaddr. In general, you need to cast the SOCKADDR_IN structure into the SOCKADDR structure and then into the system call function.
The following two data types are used in the struct sockaddr_in, which are defined as follows:
/* Type to represent a port. */
typedef uint16_t IN_PORT_T;
struct IN_ADDR is actually a 32-bit IP address.
struct IN_ADDR {
unsigned long s_addr;
};