Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14 bytes of Protocol address */
};
Sa_family is the address family, generally in the form of "af_xxx. It seems that af_inet is commonly used.
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.
Sockaddr_in (defined in netinet/in. h ):
Struct sockaddr_in {
Short int sin_family;/* address family */
Unsigned short int sin_port;/* Port Number */
Struct in_addr sin_addr;/* Internet address */
Unsigned char sin_zero [8];/* same size as struct sockaddr */
};
Struct in_addr {
Unsigned long s_addr;
}; 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;
Unsigned long s_addr;
} S_un;
} In_addr; sin_family indicates 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.
S_addr the IP address sockaddr_in and sockaddr are stored in parallel in byte sequence. The pointer to the addr_in struct can also point
Add struct and replace it. That is to say, you can use sockaddr_in to create the information you need,
Bzero (char *) & mysock, sizeof (mysock) can be used for type conversion at the end; // Initialization
Mysock struct name
Mysock. sa_family = af_inet;
Mysock. sin_addr.s_addr = inet_addr ("192.168.0.1 ");
......
To use the following code for conversion:
(Struct sockaddr *) The meaning of mysocksa_data is determined by sa_family.
If sa_family = af_inet
Then sa_data is the sin_addr and sin_port of sockaddr_in.
In other words, sockaddr can be viewed as sockaddr_in.
Sockfd is the socket descriptor returned by the socket function. my_addr is a sockaddr type that points to information including the local IP address and port number.
Needle; addrlen is often set to sizeof (struct sockaddr ).
The structsockaddr structure type is used to save socket information:
Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14-byte Protocol address */
};
Sa_family is generally af_inet, representing the Internet (TCP/IP) address family;
Sa_data contains the IP address and port number of the socket.
There is also a structure type:
Struct sockaddr_in {
Short int sin_family;/* address family */
Unsigned short int sin_port;/* Port Number */
Struct in_addr sin_addr;/* IP Address */
Unsigned char sin_zero [8];/* fill 0 to keep the same size as struct sockaddr */
}; This structure is more convenient to use. Sin_zero is used to fill the sockaddr_in structure with the struct
The same length of sockaddr can be set to zero using the bzero () or memset () function. The pointer pointing to sockaddr_in and the pointer pointing to sockaddr can be converted to each other, which means that if the parameter type required for a function is sockaddr, you can call a function to point
The addr_in pointer is converted to the sockaddr pointer; or vice versa. |