.NET2 DNS 拒绝超过 126 个字符的主机名,因为它“太长”。
在开发一个程序时,我最近发现 .net 中的主机名(或至少在 ping 类中)不应超过 126 个字符。如果主机名较长,则 ping 类会引发异常。
然而 Wikipedia 规定最多允许 255 个字符。 看起来确实有主机名超过 126 个字符的机器,所以问题是:这个限制可以改变吗?谁是对的?如果不能改变如何解析名称?
While working on a program I recently found that hostnames in .net (or at least in the ping class) are not supposed to have more than 126 characters. The ping class throws an exception if a hostname is longer.
However Wikipedia states that up to 255 characters are allowed.
And it looks that indeed there are machines with a hostname longer than 126 chars out there, so the question is: can this limit be changed, who is right and how to resolve names if it cannot?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
两个信息都是正确的。
255 个字符的限制是指整个主机名(例如
some.thing.example.com
)。反过来,每个标签(例如
example
或com
)的长度限制为 63 个字符。因此,顶级域名理论上有126
个非点字符的限制。Both informations are right.
The 255 character limit refers to the entire hostname (e.g.
some.thing.example.com
).In turn, each label (e.g.
example
orcom
) is limited to 63 characters. So top-level domains have a theoretical limit of126
non-dot characters.显然就像乔尔和丹尼斯解释的那样。 .Net 无法解析长度超过 126 个字符的名称。
但是,如果有人遇到同样的问题,请查看 DNS Plus:
http://www.simpledns.com/dns-client-lib.aspx#download
Apparently it is like Joel and Dennis explained. .Net is not capable of resolving names longer than 126 chars.
However if somebody has the same problem, take a look here at DNS Plus:
http://www.simpledns.com/dns-client-lib.aspx#download
.NET
Dns
类的主机名硬性上限为 126 个字符(针对 .NET4 检查)。但是,您可以使用较低级别的 Win32
DnsQuery
方法使用 P/Invoke 将主机名转换为 IP 地址,然后将这些原始地址与 .NET 网络类一起使用。这是使用此方法的示例
DnsAddr
类:这是一个示例测试程序:
在我的机器上生成以下输出:
The .NET
Dns
class has a hard upper limit of 126 characters for hostnames (checked for .NET4).However, you can use the lower-level Win32
DnsQuery
method using P/Invoke to translate host names into IP addresses and then use those raw addresses with the .NET networking classes.Here is a sample
DnsAddr
class using this approach:Here is a sample test program:
which on my machine produces this output:
调用
gethostbyname
,然后将 IP 地址(即使对于 IPv6,也不会超过几十个字符)传递给 ping 类。
Call
gethostbyname
, then pass an IP address (which is never more than a couple dozen characters, even for IPv6) to the ping class.