struct sockaddr_un vs. sockaddr

Pavitar picture Pavitar · Sep 11, 2010 · Viewed 31.5k times · Source

How is struct sockaddr different from struct sockaddr_un ?

I know that we use these structures in client-server modules, for binding the socket to the socket address. And we use a cast operator for it to accept struct sockaddr_un.

I want to know how different/similar they are, and why the cast operator?

Answer

paulsm4 picture paulsm4 · Sep 11, 2010

"struct sockaddr" is a generic definition. It's used by any socket function that requires an address.

"struct sockaddr_un" (a "Unix sockets" address) is a specific kind of address family.

The more commonly seen "struct sockaddr_in" (an "Internet socket" address) is another specific kind of address family.

The cast is what allows the sockets APIs to accept a common parameter type that will actually be any one of several different actual types.

Here's a good link that shows several different address family definitions:

http://www.cas.mcmaster.ca/~qiao/courses/cs3mh3/tutorials/socket.html