Struct sockaddr_in {
_ Uint8_t
Sin_len;
Sa_family_t
Sin_family;
In_port_t
Sin_port;
Struct in_addr sin_addr;
Char sin_zero [8];
};
Sin_family refers to the protocol family. It can only be af_inet in socket programming.
Sin_port storage port number (in bytes)
Sin_addr storage IP address, using the in_addr Data Structure
Sin_zero is an empty byte reserved to keep sockaddr and sockaddr_in data structures of the same size.
Sin_addr stores IP addresses in byte sequence.
Sockaddr_in and sockaddr are parallel structures. The pointer to the addr_in struct can also point to the ADDR struct and replace it. That is to say, you can use sockaddr_in to create the information you need, and then use it for type conversion.
Bzero (char *) & mysock, sizeof (mysock); // Initialization
Sockaddr_in mysock;
Bzero (char *) & mysock, sizeof (mysock ));
Mysock. sa_family = af_inet;
Mysock. sin_port = htons (1234); // 1234 is the port number.
Mysock. sin_addr.s_addr = inet_addr ("192.168.0.1 ");
We mentioned sockaddr above. Now let me briefly talk about it.
Struct sockaddr {unsigned short sa_family; char sa_data [14] ;}; sa_family is the address family, which is generally in the form of "AF_xxx. Generally, AF_INET stands for TCP/IP protocol families. Sa_data is a 14-byte Protocol address. This data structure uses parameters for functions such as bind, connect, recvfrom, and sendto to specify the address information. However, in general programming, we do not directly operate on this data structure, but use another data structure equivalent to sockaddr, which is the sockaddr_in we mentioned above;
We also mentioned a Data Structure struct in_addr sin_addr, which is also a brief introduction here.
Typedef struct in_addr {
Union {
Struct {unsigned char s_b1, s_b2, s_b3, s_b4;} S_un_ B;
Struct {unsigned short s_w1, s_w2;} S_un_w;
Struct {unsigned long S_addr;} S_un;
} IN_ADDR;
The in_addr struct is used to represent a 32-bit IPv4 address.
In_addr_t is generally a 32-bit unsigned long.
Each 8 bits represent a value in an IP address bit.
For example, 192.168.3.144 is recorded as 0xc0a80390, where b1 is 192, b2 is 168, b3 is 3, b4 is 144