Looking at examples about socket programming, we can see that some people use AF_INET while others use PF_INET. In addition, sometimes both of them are used at the same example. The question is: Is there any difference between them? Which one should we use?
If you can answer that, another question would be... Why there are these two similar (but equal) constants?
socket manpageIn (Unix) socket programming, we have the socket() function that receives the following parameters:
int socket(int domain, int type, int protocol); The manpage says:
The
domainargument specifies a communication domain; this selects the protocol family which will be used for communication. These families are defined in <sys/socket.h>.
And the manpage cites AF_INET as well as some other AF_ constants for the domain parameter. Also, at the NOTES section of the same manpage, we can read:
The manifest constants used under 4.x BSD for protocol families are PF_UNIX, PF_INET, etc., while AF_UNIX etc. are used for address families. However, already the BSD man page promises: "The protocol family generally is the same as the address family", and subsequent standards use AF_* everywhere.
The sys/socket.h does not actually define those constants, but instead includes bits/socket.h. This file defines around 38 AF_ constants and 38 PF_ constants like this:
#define PF_INET 2 /* IP protocol family. */ #define AF_INET PF_INET The Python socket module is very similar to the C API. However, there are many AF_ constants but only one PF_ constant (PF_PACKET). Thus, in Python we have no choice but use AF_INET.
I think this decision to include only the AF_ constants follows one of the guiding principles: "There should be one-- and preferably only one --obvious way to do it." (The Zen of Python)
This forum post links to this old message, which contains some historical information.
AF_INET refers to Address from the Internet and it requires a pair of (host, port) where the host can either be a URL of some particular website or its address and the port number is an integer.
This address family provides interprocess communication between processes that run on the same system or on different systems. Addresses for AF_INET sockets are IP addresses and port numbers. You can specify an IP address for an AF_INET socket either as an IP address (such as 130.99.
The families supported are AF_INET and AF_INET6, which is the Internet domain, and AF_UNIX, which is the local socket domain. These constants are defined in the sys/socket. h include file. The type parameter specifies the type of socket created.
A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50. 200.5' , and port is an integer.
I think the Wikipedia notes on this sum it up pretty well:
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 ofPF_. TheAF_-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 theAF_-constants were simply defined by the corresponding protocol identifier, rendering the distinction betweenAF_versusPF_constants a technical argument of no significant practical consequence. Indeed, much confusion exists in the proper usage of both forms.
Even if someone came up with a reason to have a difference today, they'd have to come up with new identifiers or so much stuff would break...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With