其他分享
首页 > 其他分享> > AF_INET还是PF_INET?

AF_INET还是PF_INET?

作者:互联网

我认为:用AF_INET好,用PF_INET也行。winsock.h里:
#define AF_INET 2
#define PF_INET AF_INET
#define PF_UNIX AF_UNIX
上面这样的宏定义有26行。
In practice, though, the PF_ and AF_ macros for the built-in protocols have the same values (in both Linux and Windows).

下面的例子都是用AF_的:

https://www.man7.org/linux/man-pages/man7/socket.7.html
#include <sys/socket.h>
sockfd = socket(int socket_family, int socket_type, int protocol); // fd: file descriptor
socket_family可以是AF_INET, AF_UNIX, AF_IPX, and AF_PACKET等。打个不太恰当的比方:同一个地址192.168.0.100上可以有HTTP和FTP等多个协议。

https://www.ibm.com/docs/en/ztpf/1.1.0.15?topic=considerations-unix-domain-sockets
#define SOCK_PATH "tpf_unix_sock.server"
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un server_sockaddr;
server_sockaddr.sun_family = AF_UNIX;
strcpy(server_sockaddr.sun_path, SOCK_PATH);

https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/
Beginning in Windows 10 Insider Build 17063, you'll be able to use the unix socket (AF_UNIX) address family on Windows to communicate between Win32 processes. Unix sockets allow inter-process communication (IPC) between processes on the same machine.

IPX is a networking protocol. Originally used by the Novell NetWare operating system and it was later adopted by Windows.

https://www.opensourceforu.com/2015/03/a-guide-to-using-raw-sockets/
For a raw socket, the socket family is AF_PACKET, the socket type is SOCK_RAW and for the protocol, see the if_ether.h header file.

历史

Berkeley sockets is an application programming interface (API) for Internet sockets and Unix domain sockets, used for inter-process communication (IPC). It is commonly implemented as a library of linkable modules. It originated with the 4.2BSD Unix operating system, released in 1983.

Berkeley sockets evolved with little modification from a de facto standard into a component of the POSIX specification. The term POSIX sockets is essentially synonymous with Berkeley sockets, but they are also known as BSD sockets, acknowledging the first implementation in the Berkeley Software Distribution.

As the Berkeley socket API evolved and ultimately yielded the POSIX socket API, certain functions were deprecated or removed and replaced by others. The POSIX API is also designed to be reentrant.

inet_aton, inet_ntoa, gethostbyname, gethostbyaddr, getservbyname, getservbyport, gethostbyaddr, getservbyport are BSD functions. inet_pton, inet_ntop, getaddrinfo, getnameinfo是POSIX函数。

All modern operating systems implement a version of the Berkeley socket interface. It became the standard interface for applications running in the Internet. Even the Winsock implementation for MS Windows, created by unaffiliated developers, closely follows the standard.

The function socket creates an endpoint for communication and returns a file descriptor for the socket. It uses three arguments:

domain [又拽名词], which specifies the protocol [没用address] family of the created socket. For example:

type, one of:

protocol specifying the actual transport protocol to use. The most common are IPPROTO_TCP, IPPROTO_SCTP, IPPROTO_UDP, IPPROTO_DCCP. These protocols are specified in file netinet/in.h. The value 0 may be used to select a default protocol from the selected domain and type.

The following lists a sampling of protocol families (preceded by the standard symbolic identifier) defined in a modern Linux or BSD implementation:

A socket for communications is created with the socket function, by specifying the desired protocol family (PF_-identifier) as an argument.

socket(int socket_family, int socket_type, int protocol)换socket(domain,  type, protocol)后,domain family, DF_或DOMAIN_不好吗?PF_很容易让人以为是传递给protocol的。而且"as an argument",socket()有3个参数,as which argument? 真是有人编程有人添乱。

六级/考研单词: protocol, socket, domain, implement, evolve, modify, component, synonym, hardware, yield, backward, compatible, pack, atop, default, precede, desire

标签:protocol,socket,AF,PF,sockets,INET
来源: https://www.cnblogs.com/funwithwords/p/15860082.html