ChatGPT解决这个技术问题 Extra ChatGPT

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?

I'm confused between using AF_INET and PF_INET in socket() and bind().

Also, how to give ip-address in sin_addr field?

Just search the net: one result is this
I've been wondering this as well. They seem to get used interchangeably in the socket call among different coders.
@MattH They both are same as per the new Linux Kernels. You can find the same in Duke's answer below.

p
pzp

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.


D
Duke

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

sure Duke, is it same for previous kernels also , i mean kernels prior to version 3.0 ?
As fas as I know, in all versions of kernel and libc, PF_* == AF_*
Is this true on non-linux platforms?
I think to make sure, you need to check the included header file :)
On Ubuntu/Debian I found AF definitions in /usr/src/linux-headers-<kernel_version>/include/linux/socket.h
u
user

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); 

thanks @codemac , i have used addr.sin_addr.s_addr = inet_addr("127.0.0.1"); but what is inet_pton() for ?
also for man pages, when i type man bind(2) or man bind() , terminal gives unexpected token '(' error whereas man bind gives explanation of bind in bash builtins. How to get man page for bind() . i mean bind() function ?
@jatt.beas: The syntax is man <section> <topic>, e.g. man 2 bind.
f
firo

In fact, AF_ and PF_ are the same thing. There are some words on Wikipedia will clear your confusion

The original design concept of the socket interface distinguished between protocol types (families) and the specific address types that each may use. It was envisioned that a protocol family may have several address types. Address types were defined by additional symbolic constants, using the prefix AF_ instead of PF_. The AF_-identifiers are intended for all data structures that specifically deal with the address type and not the protocol family. However, this concept of separation of protocol and address type has not found implementation support and the AF_-constants were simply defined by the corresponding protocol identifier, rendering the distinction between AF_ versus PF_ constants a technical argument of no significant practical consequence. Indeed, much confusion exists in the proper usage of both forms.


D
DINO U

AF_INET = Address Format, Internet = IP Addresses

PF_INET = Packet Format, Internet = IP, TCP/IP or UDP/IP

AF_INET is the address family that is used for the socket you're creating (in this case an Internet Protocol address). The Linux kernel, for example, supports 29 other address families such as UNIX sockets and IPX, and also communications with IRDA and Bluetooth (AF_IRDA and AF_BLUETOOTH, but it is doubtful you'll use these at such a low level).

For the most part sticking with AF_INET for socket programming over a network is the safest option.

Meaning, AF_INET refers to addresses from the internet, IP addresses specifically.

PF_INET refers to anything in the protocol, usually sockets/ports.


S
Stefan Becker

Checking the header file solve's the problem. One can check for there system compiler.

For my system , AF_INET == PF_INET

AF == Address Family And PF == Protocol Family

Protocol families, same as address families.

https://i.stack.imgur.com/iIjqq.png


/usr/src/linux-headers-X.X.X-XX-generic/include/linux/socket.h
u
user207421

There are situations where it matters.

If you pass AF_INET to socket() in Cygwin, your socket may or may not be randomly reset. Passing PF_INET ensures that the connection works right.

Cygwin is self-admittedly a huge mess for socket programming, but it is a real world case where AF_INET and PF_INET are not identical.


Please explain. I find #define PF_INET AF_INET in Cygwin's socket.h.