What is the difference between AF_INET and PF_INET in socket programming? What is the difference between AF_INET and PF_INET in socket programming? c c

What is the difference between AF_INET and PF_INET in socket programming?


Beej's famous network programming guide gives a nice explanation:

In some documentation, you'll see mention of a mystical "PF_INET". This is a weird etherial beast that is rarely seen in nature, but I might as well clarify it a bit here. Once a long time ago, it was thought that maybe a address family (what the "AF" in "AF_INET" stands for) might support several protocols that were referenced by their protocol family (what the "PF" in "PF_INET" stands for).
That didn't happen. Oh well. So the correct thing to do is to use AF_INET in your struct sockaddr_in and PF_INET in your call to socket(). But practically speaking, you can use AF_INET everywhere. And, since that's what W. Richard Stevens does in his book, that's what I'll do here.


I found in Linux kernel source code that PF_INET and AF_INET are the same.The following code is from file include/linux/socket.h, line 204 of Linux kernel 3.2.21 tree.

/* Protocol families, same as address families. */...#define PF_INET     AF_INET


  • AF = Address Family
  • PF = Protocol Family

Meaning, AF_INET refers to addresses from the internet, IP addresses specifically. PF_INET refers to anything in the protocol, usually sockets/ports.

Consider reading the man pages for socket(2) and bind(2). For the sin_addr field, just do something like the following to set it:

struct sockaddr_in addr;inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);