C# 将主机名解析为 IP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13248971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:55:57  来源:igfitidea点击:

Resolve HostName to IP

c#

提问by Ba7a7chy

I have been through a lot of googling for this, I found a lot of examples none of which was working for me. This is a simple issue which I feel has a simple answer without defining new classes\modules etc...

我为此进行了很多谷歌搜索,我发现了很多例子,但没有一个对我有用。这是一个简单的问题,我觉得它有一个简单的答案,无需定义新的类\模块等...

My code is this :

我的代码是这样的:

Console.WriteLine ("Please enter an IP address or hostname");
string host = Console.ReadLine ();
***IP = resolved "host"*** 
Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);    
s.Connect (IP, 80);
s.close();

How do I actually resolve the IP variable?

我如何实际解析 IP 变量?

采纳答案by Kolja

You can simply use the DNSclass to do so:

您可以简单地使用DNS类来执行此操作:

IPHostEntry hostEntry;

hostEntry= Dns.GetHostEntry(host);

//you might get more than one ip for a hostname since 
//DNS supports more than one record

if (hostEntry.AddressList.Length > 0)
{
      var ip = hostEntry.AddressList[0];
      Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
      s.Connect(ip, 80);
}

回答by py2020

string howtogeek = "www.howtogeek.com";
IPAddress[] addresslist = Dns.GetHostAddresses(howtogeek);

foreach (IPAddress theaddress in addresslist)
{
   Console.WriteLine(theaddress.ToString());
 }

from howtogeek

来自howtogeek

回答by PiotrK

Please take the note that accepted answer can resolve to IPv6. I attempted to connect to service that does not accept IPv6 as input string.

请注意,接受的答案可以解析为 IPv6。我试图连接到不接受 IPv6 作为输入字符串的服务。

Therefore try this snippet if you care to get IPv4:

因此,如果您想获得 IPv4,请尝试使用此代码段:

using System.Linq;

string host = "google.com";

Dns.GetHostEntry(host).AddressList.First(addr => addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)

回答by marsh-wiggle

This is the method I use to resolve a hostname to IPv4 and / or IPv6.

这是我用来将主机名解析为 IPv4 和/或 IPv6 的方法。

    using System.Net:

    // A host can have multiple IP addresses!
    public static IPAddress[] GetIPsByName(string hostName, bool ip4Wanted, bool ip6Wanted)
    {
        // Check if the hostname is already an IPAddress
        IPAddress outIpAddress;
        if (IPAddress.TryParse(hostName, out outIpAddress) == true)
            return new IPAddress[] { outIpAddress };
        //<----------

        IPAddress[] addresslist = Dns.GetHostAddresses(hostName);

        if (addresslist == null || addresslist.Length == 0)
            return new IPAddress[0];
        //<----------

        if (ip4Wanted && ip6Wanted)
            return addresslist;
        //<----------

        if (ip4Wanted)
            return addresslist.Where(o => o.AddressFamily == AddressFamily.InterNetwork).ToArray();
        //<----------

        if (ip6Wanted)
            return addresslist.Where(o => o.AddressFamily == AddressFamily.InterNetworkV6).ToArray();
        //<----------

        return new IPAddress[0];
    }