是否可以通过使用其主机名获取计算机的IP地址? [英] is it possible to get IP address of a computer by using its hostname?

查看:147
本文介绍了是否可以通过使用其主机名获取计算机的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如主题中所述,
我试图做一个代码,使我可以使用主机名在局域网中检索计算机的IP地址.

As mention in the subject,
im trying to do a code which allow me to retrieve a IP address of a computer in LAN by using its hostname.

推荐答案

System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry("COMPUTERNAME");
foreach (var ipAddress in host.AddressList)
{
    // Loop all ip-addresses. This object has a lot of properties v4/v6 etc..
    Console.WriteLine(ipAddress);
}




请参阅此链接以获取一些代码:
http://www.dijksterhuis.org/converting -an-ip-address-to-a-hostname-and-back-with-c/ [
Hi,

see this link for some code:
http://www.dijksterhuis.org/converting-an-ip-address-to-a-hostname-and-back-with-c/[^]
I can use this code to look up the IP-Address of a server called "Panic":
private void button1_Click(object sender, EventArgs e)
{
   IPHostEntry NameToIpAddress;
   NameToIpAddress = Dns.GetHostEntry("Panic");
   int AddressCount = 0;
   foreach (IPAddress Address in NameToIpAddress.AddressList)
      Console.WriteLine("IP Address {0}: {1}", ++AddressCount, Address.ToString());
}


string strHostName="localhost";
IPHostEntry ipEntry = Dns.GetHostByName( strHostName );
IPAddress [] addr = ipEntry.AddressList;

for( int i = 0; i < addr.Length; i++ )
{
Console.WriteLine( "IP Address {0}: {1} ", i, addr [ i ].ToString() );
}


这篇关于是否可以通过使用其主机名获取计算机的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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