How to resolve a domain name to a single IP address?

眉间皱痕 提交于 2019-12-25 08:41:09

问题


I was surprised to learn that a single domain name can have many IP addresses. For example, here are my results for Dns.GetHostAddresses("www.google.com"):

{System.Net.IPAddress[6]}
    [0]: {74.125.127.147}
    [1]: {74.125.127.99}
    [2]: {74.125.127.103}
    [3]: {74.125.127.104}
    [4]: {74.125.127.105}
    [5]: {74.125.127.106}

(YMMV; the addresses seem to change periodically)

Where do these different addresses come from, and how should one choose an IP address to connect to?


回答1:


The IP addresses come from the DNS server associated with the queried domain name, www.google.com in your example. This would be the same process a web browser follows to get the server IP addresses to connect to.

As for which IP address to connect to I'd imagine they're all redundant and you should probably just connect to the first one, however this is just speculation and I don't really know for sure.




回答2:


Unless there is a reason to specifically bind to the IP, you should use DNS to resolve at the moment you make the connection.

As for the "where do they come from", thats answered by any number of infrastructure decisions. This is the power of DNS, in that load balancing, caching, delivery, etc systems can dynamically serve up a domain (example.com) from any number of sources without you (the client) having to worry about that implementation.

The multiple DNS can be multiple servers, different datacenters, cache networks, etc ... depending on any number of factors.




回答3:


There are most likely using some sort of RoundRobin.

You can only rely on a domain name, unless the domain is under your control.




回答4:


You can do a couple of things:

  1. Use System.Net.IPAddress[0] right off the bat.
  2. You can ping all 6 and see which one has the best response time, cache that value and try to use it later.


来源:https://stackoverflow.com/questions/5278366/how-to-resolve-a-domain-name-to-a-single-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!