|
struct sockaddr {
Unsigned short sa_family; / * address family, AF_xxx * /
Char sa_data [14]; / * 14-byte protocol address * /};
Sa_family is generally AF_INET; sa_data contains the IP address and port number of the socket.
There is another type of structure:
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]; / * pad 0 to keep the same size as struct sockaddr * /
};
This structure is more convenient to use. sin_zero (which is used to fill the sockaddr_in structure to the same length as struct sockaddr) should be set to zero using the bzero () or memset () function. The pointer to sockaddr_in and the pointer to sockaddr can be converted to each other, which means that if a function requires a parameter type of sockaddr, you can convert a pointer to sockaddr_in to a pointer to sockaddr when the function is called; or vice versa. sin_family is usually assigned AF_INET; sin_port and sin_addr should be converted to network byte order; sin_addr does not need to be converted.
We discuss several byte order conversion functions below:
Htons ()-"Host to Network Short"; htonl ()-"Host to Network Long"
Ntohs ()-"Network to Host Short"; ntohl ()-"Network to Host Long"
Here, h means "host", n means "network", s means "short", and l means "long". |
|