Why do we cast sockaddr_in to sockaddr when calling bind()?

No, it’s not just convention.

sockaddr is a generic descriptor for any kind of socket operation, whereas sockaddr_in is a struct specific to IP-based communication (IIRC, “in” stands for “InterNet”). As far as I know, this is a kind of “polymorphism” : the bind() function pretends to take a struct sockaddr *, but in fact, it will assume that the appropriate type of structure is passed in; i. e. one that corresponds to the type of socket you give it as the first argument.

Leave a Comment