我开始套接字编程,我一直看到这个AF_INET。

然而,我从未见过任何其他东西被用来代替它。我的老师并没有那么乐于助人,只是说“你只是需要它”。

所以我的问题是:

AF_INET的目的是什么? 有没有别的东西可以代替它? 如果不是,为什么会有呢?为了将来可能发生的变化? 如果有,是什么原因?


当前回答

AF_INET is an address family that is used to designate the type of addresses that your socket can communicate with (in this case, Internet Protocol v4 addresses). When you create a socket, you have to specify its address family, and then you can only use addresses of that type with the socket. The Linux kernel, for example, supports 29 other address families such as UNIX (AF_UNIX) sockets and IPX (AF_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进行套接字编程是最安全的选择。因特网协议v6地址也有AF_INET6。

其他回答

您需要像AF_UNIX或AF_INET这样的参数来指定您将使用哪种类型的套接字寻址来实现IPC套接字通信。AF代表地址族。

与BSD标准套接字(Python套接字模块采用)一样,地址的表示方式如下:

一个字符串用于AF_UNIX/AF_LOCAL地址族。此选项用于不需要IP地址的本地机器上的IPC。 一个对(host, port)用于AF_INET地址族,其中host是一个字符串,表示Internet域表示法中的主机名,如' dare .cwi。nl'或IPv4地址,如'100.50.200.5',port为整数。用于在Internet上的进程之间进行通信。

AF_UNIX, AF_INET6, AF_NETLINK, AF_TIPC, AF_CAN, AF_BLUETOOTH, AF_PACKET, AF_RDS是其他选项,可以用来代替AF_INET。

这个关于AF_INET和PF_INET之间差异的线程可能也很有用。

AF_INET is an address family that is used to designate the type of addresses that your socket can communicate with (in this case, Internet Protocol v4 addresses). When you create a socket, you have to specify its address family, and then you can only use addresses of that type with the socket. The Linux kernel, for example, supports 29 other address families such as UNIX (AF_UNIX) sockets and IPX (AF_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进行套接字编程是最安全的选择。因特网协议v6地址也有AF_INET6。

The primary purpose of AF_INET was to allow for other possible network protocols or address families (AF is for address family; PF_INET is for the (IPv4) internet protocol family). For example, there probably are a few Netware SPX/IPX networks around still; there were other network systems like DECNet, StarLAN and SNA, not to mention the ill-begotten ISO OSI (Open Systems Interconnection), and these did not necessarily use the now ubiquitous IP address to identify the peer host in network connections.

AF_INET的普遍替代方案(回顾一下,应该命名为AF_INET4)是AF_INET6,用于IPv6地址族。IPv4使用32位地址;IPv6使用128位地址。

您可能会看到其他一些值-但它们是不寻常的。它的存在是为了允许选择和未来的方向。套接字接口实际上非常通用——这是它在其他网络接口萎缩的地方蓬勃发展的原因之一。

生活(大部分)变得简单了——心存感激。

套接字由它们的域、类型和传输协议来描述。常见的域名有:

AF_UNIX:地址格式为UNIX路径名 AF_INET:地址格式为主机和端口号

(实际上还有许多其他选项可以用于特殊目的)。通常我们使用AF_INET进行套接字编程

参考:http://www.cs.uic.edu/ ~特洛伊/ fall99 / eecs471 / sockets.html

它定义了协议地址族。这决定了所创建的套接字的类型。 袖珍电脑支持AF_INET。

下面这页的内容相当不错 http://etutorials.org/Programming/Pocket+pc+network+programming/Chapter+1.+Winsock/Streaming+TCP+Sockets/