I’ve been studying Linux socket programming recently, and the concepts are still swirling and unsettled in my head. Can someone confirm or correct my understanding of the domain
argument to socket()
and socketpair()
: one should choose PF_LOCAL
(or PF_UNIX
) if one wants the socket communication to be strictly within the same computer, and one should choose PF_INET
if the socket communication is meant to be between different computers — is that correct?
Advertisement
Answer
No, it’s the communications domain you want to use. See the man page for socket
. For example, AF_INET
means v4 internet protocols, AF_INET6
means v6 internet protocols, AF_APPLETALK
means AppleTalk, and so forth. You almost certainly want AF_INET
or AF_INET6
.
Whether the other program you’ll be communicating with is on the same machine or not isn’t really relevant since you can communicate with the local host just fine using internet protocols.
However, there is a small performance penalty associated with using the internet domain protocols. If your application will be connecting only with other applications on the same machine, using the AF_LOCAL/AF_UNIX
domain will be faster and will offer you some additional advantages such as file-level security controls on the sockets. Just be aware that you won’t be able to use your code between different computers without modifying it if you go that route.
A good discussion of the pros and cons of this choice can be found here.