关于socket编程中getaddrinfo()函数的问题
下面是调用 getaddrinfo() 的示例
status = getaddrinfo("www.example.net","1234", &hints, &server_info);
,之后 server_info 将指向一个包含各种地址信息的链表。
我有以下问题:
由于我已经明确指定了主机名和端口号,因此我能想到的唯一地址信息是 IPv4 和 IPv6 地址。那么,链表的长度应该是2。除了它们之外,还有其他类型的地址信息吗?
谢谢。
Below is a sample invoking of getaddrinfo()
status = getaddrinfo("www.example.net","1234", &hints, &server_info);
After that, the server_info will point to a linked list with all kinds of address information.
I have the following questions:
Since I have clearly specified the host name and port number, the only address infos I can think of are IPv4 and IPv6 addresses. So, the length of the linked list should be 2. Is there any other kind of address info besides them?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该名称可以解析为多个 IPv4 或 IPv6 地址,没有什么可说的,例如,只会返回一个 IPv4 地址(例如尝试使用“www.google.com”,您可能会获得多个 IPv4 地址)。
但无论如何,我认为你的问题的基本前提是错误的。即使现在不可能返回多个 IPv4 和一个 IPv6 地址,该函数也被记录为返回一个任意长的
addrinfo
对象链接列表。因此,即使您的代码今天在每种情况下都能工作,也不能保证它明天也能继续工作。如果该函数被记录为返回任意长的链接列表,那么您需要能够处理它。The name could resolve to more than one IPv4 or IPv6 address, there is nothing to say that only one IPv4 address will be returned, for example (try it with "www.google.com" for example, you'll likely get more than one IPv4 address).
But in any case, I think the basic premise of your question is wrong. Even if there was no possibility to return more than one IPv4 and one IPv6 address today, the function is documented to return an arbitrarily-long linked list of
addrinfo
objects. Therefore, even if your code worked today in every situation, there is no guarantee that it would continue to work tomorrow. If the function is documented to return an arbitrarily-long linked list, then you need to be able to handle that.您想要断开脑海中名称的机器的物理配置。 DNS 仅将名称映射到一组地址。许多主机只有一个接口。许多主机都会有多个(称为多宿主)。 DNS 不关心将名称映射到的地址的机器的配置是什么。作为简单的示例,服务器通常在多个网络上具有具有不同地址的接口,这些接口将全部映射到相同的名称。有时,当将来自不同机器的服务折叠到一台不同的名称时,会映射到相同的地址。因此,不要假设名称和机器之间存在任何形式的 1:1 映射,更不用说接口了。
You want to disconnect the physical configuration of machines with names in your mind. DNS merely maps a name to a set of addresses. A lot of hosts will only have one interface. Many hosts will have multiple (called multi-homing). DNS doesn't care about what the configuration of the machine or machines that the addresses it maps a name to is. As simple examples often a server will have interfaces on multiple networks with different addresses that will all map to the same name. Sometimes when collapsing services from different machines onto one different names will map to the same address. So don't assume any sort of 1:1 mapping between names and machines much less interfaces.