ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

AF_INET还是PF_INET?

2022-02-02 10:00:10  阅读:292  来源: 互联网

标签:protocol socket AF PF sockets 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:

  • AF_INET for network protocol IPv4 (IPv4-only)
  • AF_INET6 for IPv6 (and in some cases, backward compatible with IPv4)
  • AF_UNIX for local socket (using a special filesystem node)

type, one of:

  • SOCK_STREAM (reliable stream-oriented service or Stream Sockets)
  • SOCK_DGRAM (datagram service or Datagram Sockets)
  • SOCK_SEQPACKET (reliable sequenced packet service)
  • SOCK_RAW (raw protocols atop the network layer)

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:

  • PF_LOCAL, PF_UNIX, PF_FILE Local to host (pipes and file-domain)
  • PF_INET Internet Protocol version 4
  • ...
  • PF_BLUETOOTH Bluetooth sockets

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有