是否有用于 ipv4 和 ipv6 地址的标准 .NET 解码器?
我想编写一个相当简单的客户端-服务器网络应用程序。我只使用纯 IPv4 网络,但如果我的代码能够面向未来,那就太好了。我可能会使用 TcpListener/TcpClient,因为WCF 的初步调查表明它的设置过于复杂且难以理解。
对于客户端,.NET 是否提供了自动解码包含 IPv4 或 IPv6 地址(其中 IPv4 地址包含端口号)的字符串的功能?如果可以解析域名则加分。
对于服务器端,我听说 IPv6 不使用端口号,那么要侦听的端口的等效项是什么?是否有标准方法来区分 IPv4 端口号字符串和 IPv6 等效端口号字符串? 没关系,IPv6 服务和 IPv4 一样也有 16 位端口号。
I'd like to write a fairly simple client-server network app. I only use a pure IPv4 network, but it would be nice to future-proof my code. I'll probably be using TcpListener/TcpClient, because preliminary investigation of WCF reveals it to be overly complex to set up and hard to grok.
For the client side, does .NET provide a facility to automatically decode a string that contains an IPv4 or IPv6 address (where the IPv4 address includes a port number)? Bonus points if it can resolve a domain name.
For the server side, I heard IPv6 doesn't use port numbers, so what's the equivalent of the port to listen on, and is there a standard way to differentiate a IPv4 port number string from the IPv6 equivalent? Never mind, IPv6 services have 16-bit port numbers just like IPv4 ones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的 System.Net.IPAddress
并测试 IPv4或 v6
Yep System.Net.IPAddress
And to test for IPv4 or v6
System.Net.IPAddress 可用于解析表示有效 IPv4 和 IPv6 地址的字符串。
System.Net.Dns 可用于解析网络上的主机名和地址。
对于两者来说,
System.Net.IPAddress can be used to parse strings representing valid IPv4 and IPv6 addresses.
System.Net.Dns can be used to resolve hostnames and addresses on a network.
For both,
@Qwertie 说:
正如其他人所指出的,端口号不是 IP 地址(IPv4 或 IPv6)的一部分。 IP 地址是原子的无符号整数(IPv4 是 32 位,IPv6 是 128 位)。当以字符形式(例如 URI)表示时,它们可以与端口号(和其他信息)组合。在 URI 中,主机和端口[可以]是 URI 的权限部分的一部分。
URI 可以使用
System.Uri
。 URI 的
Authority
部分由以下属性组成:Host
、Port
(以及可选且已弃用的 UserInfo 子组件,由用户名和密码)。属性HostNameType
将告诉您所拥有的主机名类型,枚举UriHostNameType
的值:Dns、Ipv4、Ipv6 或其他值。您还可以使用 RFC 3986 的附录 B 中定义的正则表达式将 URI 解析为其组成部分:
但请注意,这个正则表达式似乎有点不正确。根据 RFC 3986 及其前身 RFC 2396 的勘误表:
@Qwertie said:
As others have noted, a port number is not part of an IP address (either IPv4 or IPv6). IP addresses are atomic, unsigned integers (IPv4 is 32-bit, IPv6 is 128-bit). When represented in character form (such as a URI), they may be combined with a port number (and other information). Within a URI, the host and port are [can be] a part of the authority portion of a URI.
URIs may be parsed into their constituent parts using
System.Uri
. TheAuthority
portion of a URI is composed of the following properties:Host
,Port
(and, optionally, and deprecated, the UserInfo subcomponent, consisting of username and password). The propertyHostNameType
will tell you what kind of hostname you've got, a value of the enumUriHostNameType
: Dns, Ipv4, Ipv6 or something else.You can also parse a URI into its component parts using the regular expression defined in Appendix B of RFC 3986:
Note though that this regular expression seems to be slightly incorrect. According to the errata for RFC 3986 and its antecedent, RFC 2396:
正如 Paul 提到的,没有端口号的纯 IP 地址可以通过 IPAddress.Parse() 进行解析。但是,如果存在端口号和/或主机名(12.34.56.78:90 或 www.example.com:5555),则需要采用不同的方法。如果您想使用 TcpClient 进行连接,此函数将这样做:
defaultPort
参数指定在输入字符串没有端口时要使用的端口。例如:要在不连接的情况下解码地址(例如,验证它是否有效,或者因为您通过需要 IP 地址的 API 进行连接),此方法将这样做(并可选择执行 DNS 查找):
奇怪的是,
Dns.GetHostAddresses
可以返回多个地址。我询问了,并且显然,只需获取第一个地址就可以了。如果存在语法错误或解析域名时出现问题(
FormatException
或SocketException
),则会引发异常。如果用户指定了域名,但resolveDns==false
,则此方法返回null
。As Paul mentioned, a plain IP Address with no port number can be parsed by
IPAddress.Parse()
. However, if there is a port number and/or hostname (12.34.56.78:90 or www.example.com:5555), a different approach is needed. If you want to use TcpClient to connect, this function will do so:The
defaultPort
parameter specifies the port to use if the input string doesn't have one. For example:To decode the address without connecting to it (e.g. to verify that it is valid, or because you are connecting via an API that requires an IP address), this method will do so (and optionally perform DNS lookup):
Curiously,
Dns.GetHostAddresses
can return multiple addresses. I asked about it, and apparently it's okay to simply take the first address.An exception will be raised if there is a syntax error or a problem resolving the domain name (
FormatException
orSocketException
). If the user specified a domain name butresolveDns==false
, this method returnsnull
.