Linux socket编程中AF_INET和PF_INET有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6729366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between AF_INET and PF_INET in socket programming?
提问by SP Sandhu
What is the difference between AF_INET and PF_INET in socket programming?
socket编程中AF_INET和PF_INET有什么区别?
I'm confused between using AF_INET and PF_INET in socket()
and bind()
.
我对在socket()
和 中使用 AF_INET 和 PF_INET 感到困惑bind()
。
Also, how to give ip-address in sin_addr
field?
另外,如何在sin_addr
字段中提供 ip-address ?
采纳答案by Damon
Beej's famous network programming guidegives a nice explanation:
Beej 著名的网络编程指南给出了很好的解释:
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.
在某些文档中,您会看到提到了神秘的“PF_INET”。这是一种自然界中很少见的怪异灵兽,不过我还是在这里稍微澄清一下。很久以前,人们认为也许一个地址族(“AF_INET”中的“AF”代表什么)可能支持其协议族引用的几种协议(“PF_INET”中的“PF”代表什么) )。
那没有发生。那好吧。所以正确的做法是在你的 struct sockaddr_in 和 PF_INET 中使用 AF_INET 在你对 socket() 的调用中。但实际上,您可以在任何地方使用 AF_INET。而且,因为这就是 W. Richard Stevens 在他的书中所做的,这就是我在这里要做的。
回答by codemac
- AF = Address Family
- PF = Protocol Family
- AF = 地址族
- PF = 协议族
Meaning, AF_INET
refers to addresses from the internet, IP addresses specifically. PF_INET
refers to anything in the protocol, usually sockets/ports.
意思AF_INET
是指来自互联网的地址,特指 IP 地址。PF_INET
指协议中的任何内容,通常是套接字/端口。
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:
考虑阅读socket(2)和bind(2)的手册页。对于该sin_addr
字段,只需执行以下操作即可对其进行设置:
struct sockaddr_in addr;
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
回答by 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.
我在 Linux 内核源代码中发现 PF_INET 和 AF_INET 是相同的。以下代码来自文件include/linux/socket.h,Linux 内核 3.2.21 树的第 204 行。
/* Protocol families, same as address families. */
...
#define PF_INET AF_INET
回答by firo
In fact, AF_ and PF_ are the same thing. There are some words on Wikipedia will clear your confusion
其实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 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.
套接字接口的原始设计概念区分了协议类型(族)和每个可能使用的特定地址类型。设想一个协议族可能有几种地址类型。地址类型由附加符号常量定义,使用前缀 AF_ 而不是 PF_。AF_标识符用于专门处理地址类型而不是协议族的所有数据结构。然而,这种协议和地址类型分离的概念还没有找到实现支持,AF_常量只是由相应的协议标识符定义,使得 AF_ 与 PF_ 常量之间的区别成为没有重大实际后果的技术论点。事实上,在正确使用这两种形式时存在很多混淆。
回答by FlyingJester
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.
如果您socket()
在 Cygwin 中将AF_INET 传递给,则您的套接字可能会或可能不会随机重置。传递 PF_INET 可确保连接正常工作。
Cygwin is self-admittedly a huge mess for socket programming, but it isa real world case where AF_INET and PF_INET are not identical.
不可否认,Cygwin 对于套接字编程来说是一个巨大的混乱,但它是AF_INET 和 PF_INET 不相同的真实世界案例。
回答by DINO U
AF_INET = Address Format, Internet = IP Addresses
AF_INET = 地址格式,Internet = IP 地址
PF_INET = Packet Format, Internet = IP, TCP/IP or UDP/IP
PF_INET = 数据包格式,Internet = IP、TCP/IP 或 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).
AF_INET 是用于您正在创建的套接字的地址族(在本例中为 Internet 协议地址)。例如,Linux 内核支持 29 个其他地址族,例如 UNIX 套接字和 IPX,以及与 IRDA 和蓝牙(AF_IRDA 和 AF_BLUETOOTH,但您是否会在如此低的级别使用它们)的通信。
For the most part sticking with AF_INET for socket programming over a network is the safest option.
在大多数情况下,坚持使用 AF_INET 进行网络套接字编程是最安全的选择。
Meaning, AF_INET refers to addresses from the internet, IP addresses specifically.
意思是,AF_INET 是指来自互联网的地址,特别是 IP 地址。
PF_INET refers to anything in the protocol, usually sockets/ports.
PF_INET 指的是协议中的任何东西,通常是套接字/端口。
回答by Rishap Singh
Checking the header file solve's the problem. One can check for there system compiler.
检查头文件解决了问题。可以检查那里的系统编译器。
For my system , AF_INET == PF_INET
对于我的系统,AF_INET == PF_INET
AF == Address Family And PF == Protocol Family
AF == 地址族和 PF == 协议族
Protocol families, same as address families.
协议族,与地址族相同。