I'm building a simple socket web server using the sys/socket.h lib, and I came across the socklen_t and sa_family_t data types and am a bit confused on what their actual purpose is.
Definition:
sa_family_t - unsigned integer type.socklen_t - an unsigned opaque integer type of length of at least 32-bits.Now I understand that the <sys/socket> lib declares three structures (sockaddr,msghdr,cmsghdr)  which contain members that declare these data  types.
sa_family_t   sa_family       address familysocklen_t     msg_namelen     size of addresssocklen_t     msg_controllen  ancillary data buffer lensocklen_t     cmsg_len        data byte count, including the cmsghdrBut why create new data types, why not just use an unsigned int data type?
DESCRIPTION. <sys/socket. h> makes available a type, socklen_t, which is an unsigned opaque integral type of length of at least 32 bits.
sa_family_t should be an unsigned integer. The Windows header files don't conform to that standard. Winsock. h defines the sockaddr struct as follows: struct sockaddr { u_short sa_family; /* address family */ char sa_data[14]; /* up to 14 bytes of direct address */ };
DESCRIPTION. The <sys/socket. h> header shall define the type socklen_t, which is an integer type of width of at least 32 bits; see APPLICATION USAGE.
By declaring specific types for these fields, it decouples them from a particular representation like unsigned int.  
Different architectures can be free to define different sizes for these fields, and code that uses these specific types doesn't need to worry about how big an int is on a given machine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With