解析主机名到IP [英] Resolve HostName to IP

查看:262
本文介绍了解析主机名到IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了很多使用Google的这一点,我发现了很多的例子没有一个是为我工作。这是一个简单的问题,我觉得有一个简单的答案,但没有设定新的classes\modules等等...

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...

我的代码是这样的:

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();



我如何真正解决IP变量?

How do I actually resolve the IP variable?

推荐答案

您可以简单地使用 DNS 类这样做的:

You can simply use the DNS class to do so:

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);
}

这篇关于解析主机名到IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆