【发布时间】:2021-03-25 03:06:53
【问题描述】:
我正在尝试遍历范围 (192.168.1.1 - 192.168.1.255) 内的所有 IP 地址并打印出包含 X 主机名的 IP 地址。
我正在尝试使用此代码,但它只给了我最后一个包含 X 主机名的 IP 地址,但有多个 IP 地址与此主机名:
string customPc = "myCustomPc";
IPHostEntry host = Dns.GetHostEntry(customPc);
foreach (IPAddress theaddress in host.AddressList)
{
Console.WriteLine(theaddress.ToString());
}
另外,我正在尝试这样做,但它打印出相同的结果 - 具有此主机名的最后一个 IP 地址:
string host = "myCustomPc";
IPHostEntry hostEntry;
hostEntry = Dns.GetHostEntry(host);
if (hostEntry.AddressList.Length > 0)
{
var ip = hostEntry.AddressList[0];
Console.WriteLine(ip + ": Your custom PC is found!");
}`
【问题讨论】:
标签: c# asp.net network-programming ip-address hostname