C#Dns.GetHostEntry不返回连接到WiFi的移动设备的名称 [英] C# Dns.GetHostEntry doesn't return names of mobile devices connected to WiFi

查看:169
本文介绍了C#Dns.GetHostEntry不返回连接到WiFi的移动设备的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有一个Windows窗体应用程序,我试图获取列表中所有客户端的主机名.下面给出的是ra00l通过此链接提供的代码示例: GetHostEntry非常慢(我有一个做出了类似的代码,但是这个代码更干净)

I have a windows form application in C# and I'm trying to get the host name of all the clients that I have in a list. Given below is a code example by ra00l from this link: GetHostEntry is very slow (I have a similar code made but this one is cleaner)

private delegate IPHostEntry GetHostEntryHandler(string ip);
public string GetReverseDNS(string ip, int timeout) 
{
     try
     {
          GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
          IAsyncResult result = callback.BeginInvoke(ip,null,null);
          if (result.AsyncWaitHandle.WaitOne(timeout, false))
          {
               return callback.EndInvoke(result).HostName;
          }
          else 
          {
               return ip;
          }
     } 
     catch(Exception)
     {
          return ip;
     }
}

在网络中为其指定Windows计算机的IP时,如果输入合理的超时时间,它将显示正确的主机名.经过我的测试,我没有收到关于android和apple设备的主机名的任何响应.例如,下面的图片是我拥有的路由器的DHCP客户端列表.它显示了android,苹果和笔记本电脑设备.我正在使用笔记本电脑"Nathu-Laptop"为我提供IP地址"192.168.1.106".

When it's given an IP of a Windows Machine in the network, it shows the correct host name given that you enter a reasonable timeout. After the tests that I made, I didn't get any response for the host names of android and apple devices. For example, the picture below is the DHCP Client List of the router that I have. It shows android, apple and laptop devices. I'm using the laptop 'Nathu-Laptop' giving me an IP address of '192.168.1.106'.

如果我在C#函数中输入"192.168.1.105",则结果为"Nandwani-PC",但如果我输入"192.168.1.103","192.168.1.104","192.168.1.101","192.168.1.100" ',我没有任何主机名.

If I enter '192.168.1.105' in the C# function, the result is 'Nandwani-PC' but if I input '192.168.1.103', '192.168.1.104', '192.168.1.101', '192.168.1.100', I don't get any hostname.

我也尝试使用nbtstat,但它只能使笔记本电脑进入网络.

I also tried using nbtstat but it only gets the laptops in the network.

在iPod上尝试此操作时,请确保正在进行网络活动.这是为了使连接保持活动状态,因为它在待机状态下会与网络断开连接.

When trying this out on my iPod, I ensure that there is a network activity going on. This is to keep the connection alive because it disconnects from the network when it's on standby.

所以我发现DNS.GetHostEntry调用 getaddrinfo (如果启用了IPv6),否则,调用

So I found out that DNS.GetHostEntry calls getaddrinfo if IPv6 is enabled, otherwise, call gethostbyaddr and these functions may access the data from \System32\drivers\etc\hosts or maybe from the NETBIOS. The thing is that the NETBIOS is legacy right? but how about for mobile devices?

推荐答案

关于NetBIOS:

为了回答有关NetBIOS和网络名称解析的特定问题,我将提供更多详细信息.如果网络上没有运行dns服务器,则名称解析将仅取决于NetBIOS解析. 这是一个标准 ,并且已在多个操作系统上实现.但是,速度不是很快.

In order to answer your specific questions about NetBIOS, and name resolution on network, I'll give more details. If you don't have dns server running on your network, name resolution will rely only on NetBIOS resolution. It is a standard and implemented on several operating systems. However, it's not very fast.

即使我们很老,我们也不是过去的遗留和过时的

您可以检查 Microsoft支持在Windows和Windows上解析名称的方式. NetBIOS是最后一个.

You can check on Microsoft support the way names are resolved on Windows and NetBIOS is the last one.

但是,NetBios名称解析并不总是可以完全正常运行(例如此错误在所有移动平台(例如,取决于Android版本)上于2014年修复的Android 上. 如果要提高性能,建议您在网络中安装DNS服务器.

However, NetBios name resolution is not always fully functional (like this bug on Android which was fixed in 2014) on all mobile platforms (depends on Android version for example). If you want to improve the performance, I suggest you to install a DNS server in the network.

您是否尝试过ping -a <IP address>nslookup <IP address>来检查结果是否符合您的期望?

Did you try ping -a <IP address> or nslookup <IP address> to check if the results are in line with your expectations ?

如果问题仍然存在,则可以使用上述链接来研究.Net的实现.您还可以检查 a此处更多.Net DNS实施的最新版本

If your problem still persist, then you may investigate the .Net implementation, thanks to the above links. You can also check a more up to date version of .Net DNS implementation here

这篇关于C#Dns.GetHostEntry不返回连接到WiFi的移动设备的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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