Data Type - socklen_t, sa_family_t

15,483

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.

Share:
15,483
Jordan Davis
Author by

Jordan Davis

If you rely on frameworks don't state you know how to code, because if you did, you wouldn't be using one.

Updated on August 26, 2022

Comments

  • Jordan Davis
    Jordan Davis over 1 year

    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 family
    • socklen_t msg_namelen size of address
    • socklen_t msg_controllen ancillary data buffer len
    • socklen_t cmsg_len data byte count, including the cmsghdr

    But why create new data types, why not just use an unsigned int data type?

  • Jordan Davis
    Jordan Davis over 8 years
    Ok yea thats makes sense... so basically when it says "at least" 32-bits, it's implying that it "could" be greater, and that would be arch based, correct?
  • dbush
    dbush over 8 years
    Correct. Similarly for size_t as well.
  • Jordan Davis
    Jordan Davis over 8 years
    Cool perfect, and makes sense why it recommends not using anything higher than 2^32-1 if you want to port it between 32/64-bit systems. Thanks for the help I really appreciate it!