C#-Dns.GetHostEntry:主机名为空或不正确 [英] C# - Dns.GetHostEntry: host name is empty or not correct

查看:499
本文介绍了C#-Dns.GetHostEntry:主机名为空或不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PC已连接到网关为80.0.0.1的LAN 当我使用此代码时:

My PC is connected to a LAN which gateway is 80.0.0.1 When I use this code:

IPHostEntry hostEntry = Dns.GetHostEntry("80.0.0.1");
Var name = hostEntry.HostName;

它为我提供了一个全球Internet域的名称(hari-core-2a-xe-800-0.network.virginmedia.net),而不是我的本地主机的名称. 如何获得本地主机的正确名称. 我知道有些保留域可以与LAN一起使用,例如10.0.x.x,但是我不想更改LAN网关.如果我可以在LAN中进行更改,则如果存在相似的冲突,就不能强迫我的应用程序的所有用户更改他们的应用程序. 有什么解决办法吗?

it gives me the name of a global Internet domain (hari-core-2a-xe-800-0.network.virginmedia.net), not the name of my local host. How can I get the right name of the local host. I know that there are reserved domains like 10.0.x.x that can be used with LANs, but I don't want to change the LAN gateway. If I can change it in my LAN, I can't force all users of my application to change theirs if they have similar conflictions. Is there any work around?

我将LAN更改为9.0.0.1进行测试. Dns.GetHostEntry引发了未知此类主机"的异常.无法获取AP的名称.和连接到局域网的android设备.有什么办法吗? 注意:Advanced IP Scanner也无法获取设备名称.它会显示每个设备的IP名称.

I changed the LAN to 9.0.0.1 for testing. Dns.GetHostEntry threw "No such host is known" exception. It fails to get the name of the AP. and android devices connected to the LAN. Any solution? Note: Advanced IP Scanner also can't get the names of the devices. It displays tha IP ot each device as a name.

推荐答案

最适合我的解决方案: 1-我将LAN的网关IP更改为10.0.1.1,以避免发生冲突. 2-我使用DNS.GetHostEntry来获取名称. 3-如果名称为空(在某些AP型号中在LAN中为空)或等于IP地址,我想这是AP或路由器的IP.我将尝试连接到网页http:\ [ip],并使用网页标题作为名称(标题将是设备型号). 这适用于显示登录页面的设备.如果设备希望随请求提供用户名和密码,则会发生WebException并得到响应.Headers ["WWW-Authenticate"]包含字符串:Basic realm ="X",其中X是描述设备. 4-如果名称仍然为空,我得到的MAC地址将其显示为名称.

The solution that works for me: 1- I changed the gateway IP for my LAN to 10.0.1.1 to avoid conflictions. 2- I use DNS.GetHostEntry to get the name. 3- If name is empty (happens in LAN with some AP models) or equals to the IP address, I suppose this is the IP of an AP or a router. I will try to connect to the webpage http:\[ip] and use the title of the webpage as a name (The title will be the device model). This works with devices that displays login page. If the device wants to supply the username and password with the request, an WebException occurs and I can get the response.Headers["WWW-Authenticate"] which contains the string: Basic realm="X", where X is a string that describes the device. 4- If name is still empty, I get the MAC address show it as a name.

您可以在以下IPScanner应用程序的PingThread类中找到代码(C#& VB.NET): http://www.mediafire.com/file/1fku7fz7ytr8p59/IPScanner.zip 希望能帮助到你. 谢谢.

You can find the code (C# & VB.NET) in the PingThread Class in the IPScanner Applicationm here: http://www.mediafire.com/file/1fku7fz7ytr8p59/IPScanner.zip Hope it helps. Thanks.

这篇关于C#-Dns.GetHostEntry:主机名为空或不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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