什么是AF_INET和PF_INET常数之间有什么区别? [英] What is the difference between AF_INET and PF_INET constants?
问题描述
综观有关套接字编程的例子,我们可以看到,有些人用 AF_INET
而其他人使用 PF_INET
。此外,有时<一href=\"http://stackoverflow.com/questions/2384562/double-udp-socket-binding-in-linux/2384605#2384605\">both它们中的相同的例子的被使用。现在的问题是:有没有它们之间有什么区别?我们应该使用哪一个?
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?
在(UNIX)socket编程,我们有收到以下参数插座()
功能:
In (Unix) socket programming, we have the socket()
function that receives the following parameters:
int socket(int domain, int type, int protocol);
本手册页说:
的域
参数指定的通信区域;这个选择
协议族将被用于通信。这些家庭
上述&lt定义; SYS / socket.h中方式&gt;
The
domain
argument specifies a communication domain; this selects the protocol family which will be used for communication. These families are defined in <sys/socket.h>.
和联机帮助页引用 AF_INET
以及一些其他的 AF _
为域常数
参数。此外,在同一手册页的注意
部分,我们可以看到:
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:
下4.x的BSD用于协议族清单常数
PF_UNIX,PF_INET等,同时AF_UNIX等被用于地址的家庭。
然而,已经是BSD手册页的承诺:本协议族
通常是相同的地址家庭,和随后的标准
使用AF_ *无处不在。
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.
的 SYS / socket.h中
实际上并没有定义这些常量,而是包括位/ socket.h中
。该文件定义了约38 AF _
常数和38 PF _
这样的常量:
The C headers
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
的Python
借助 Python的插座模块是非常相似的C API。然而,也有许多 AF _
常量,但只有一个 PF _
常数(PF_PACKET)。因此,在Python中我们没有选择,只能使用 AF_INET
。
Python
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
.
我认为这个决定只包括 AF _
常量遵循的指导原则之一:应该有one--和preferably只有一个--obvious办法做到这一点。 (Python中的禅)
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.
推荐答案
我觉得维基百科这个笔记概括起来pretty得好:
I think the Wikipedia notes on this sum it up pretty well:
协议类型(家庭),并且每个可以使用特定的地址类型区分套接字接口的最初的设计理念。有人设想,一个协议族可能有几个地址类型。地址类型被附加符号常量的定义,使用preFIX AF _
而不是 PF _
。在 AF _
-identifiers旨在为专门与地址类型,而不是协议系列处理所有的数据结构。然而,协议和地址类型的分离这个概念还没有发现实施支持和 AF _
-constants被简单地通过相应的协议标识符定义,渲染的区别 AF _
与 PF _
常数没有显著实际后果的技术参数。事实上,许多混乱存在于两种形式的正确用法。
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...
这篇关于什么是AF_INET和PF_INET常数之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!