Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14 bytes of Protocol address */
};
This data structure uses parameters for functions such as bind, connect, recvfrom, and sendto to specify the address information.
But in general programming, instead of directly operating on this data structure, it uses another addr_in data structure equivalent to sockaddr (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 */
};
In programming, most of them use the sockaddr_in structure to set/obtain address information.
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
Struct in_addr {
Unsigned long s_addr;
};
This data structure is retained for historical reasons and is mainly used for compatibility with previous formats.
S_addr stores IP addresses in byte sequence.
Sin_zero is an empty byte reserved to keep sockaddr and sockaddr_in data structures of the same size.
Example of setting address information:
struct sockaddr_in SA;
SA. sin_family = af_inet;
SA. sin_port = htons (3490);/* short, nbo */
SA. sin_addr.s_addr = inet_addr ("132.241.5.10");
bzero (& (SA. sin_zero), 8);
Note: If SA. sin_addr.s_addr = inaddr_any, no IP address is specified (for server Program )