Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the difference between AF_INET and PF_INET constants?
    primarykey
    data
    text
    <p>Looking at examples about socket programming, we can see that some people use <code>AF_INET</code> while others use <code>PF_INET</code>. In addition, sometimes <a href="https://stackoverflow.com/questions/2384562/double-udp-socket-binding-in-linux/2384605#2384605">both of them are used at the same example</a>. The question is: Is there any difference between them? Which one should we use?</p> <p>If you can answer that, another question would be... Why there are these two similar (but equal) constants?</p> <hr> <h2>What I've discovered, so far:</h2> <h3>The <code>socket</code> manpage</h3> <p>In (Unix) socket programming, we have the <code>socket()</code> function that receives the following parameters:</p> <pre><code>int socket(int domain, int type, int protocol); </code></pre> <p>The manpage says:</p> <blockquote> <p>The <code>domain</code> argument specifies a communication domain; this selects the protocol family which will be used for communication. These families are defined in &lt;sys/socket.h&gt;.</p> </blockquote> <p>And the manpage cites <code>AF_INET</code> as well as some other <code>AF_</code> constants for the <code>domain</code> parameter. Also, at the <code>NOTES</code> section of the same manpage, we can read:</p> <blockquote> <p>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.</p> </blockquote> <h3>The C headers</h3> <p>The <code>sys/socket.h</code> does not actually define those constants, but instead includes <code>bits/socket.h</code>. This file defines around 38 <code>AF_</code> constants and 38 <code>PF_</code> constants like this:</p> <pre><code>#define PF_INET 2 /* IP protocol family. */ #define AF_INET PF_INET </code></pre> <h3>Python</h3> <p>The <a href="http://docs.python.org/library/socket.html" rel="nofollow noreferrer">Python socket module</a> is very similar to the C API. However, there are many <code>AF_</code> constants but only one <code>PF_</code> constant (PF_PACKET). Thus, in Python we have no choice but use <code>AF_INET</code>.</p> <p>I think this decision to include only the <code>AF_</code> constants follows one of the guiding principles: "There should be one-- and preferably only one --obvious way to do it." <a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow noreferrer">(The Zen of Python)</a></p> <h3>Other info</h3> <p><a href="http://www.developerweb.net/forum/showpost.php?p=17391&amp;postcount=5" rel="nofollow noreferrer">This forum post</a> links to <a href="http://www.kohala.com/start/lanciani.96apr10.txt" rel="nofollow noreferrer">this old message</a>, which contains some historical information.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload