是否有一个标准的.NET德codeR IPv4和IPv6地址?有一个、地址、标准、codeR

2023-09-05 23:15:35 作者:〃眼泪不快乐。

我想编写一个非常简单的客户端 - 服务器的网络应用程序。我只用纯IPv4网络,但它会是不错的面向未来的我的code。我可能会使用的TcpListener / TcpClient的,因为WCF 揭示它是过于复杂的设置和硬神交

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.

有关客户端,并.NET都提供了包含IPv4或IPv6地址(其中IPv4地址包括端口号),自动去codeA字符串?奖励积分,如果它能够解析域名。

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.

对于服务器端,我听到的IPv6不使用的端口号,有啥端口上侦听的当量,是有区分从IPv6相当于IPv4的端口号字符串的标准方式? 没关系,IPv6服务有16位端口号只是IPv4一样的。

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.

推荐答案

正如保罗所说,​​没有端口号,一个普通的IP地址可以通过解析IPAddress.Parse()。但是,如果有一个端口号和/或主机名(12.34.56.78:90或www.example.com:5555),一种不同的方法是必要的。如果你想使用TcpClient的连接,这个功能会做到这一点:

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:

public static TcpClient Connect(string ipAndPort, int defaultPort)
{
    if (ipAndPort.Contains("/"))
        throw new FormatException("Input should only be an IP address and port");

    // Uri requires a prefix such as "http://", "ftp://" or even "foo://".
    // Oddly, Uri accepts the prefix "//" UNLESS there is a port number.
    Uri uri = new Uri("tcp://" + ipAndPort);

    string ipOrDomain = uri.Host;
    int port = uri.Port != -1 ? uri.Port : defaultPort;
    return new TcpClient(ipOrDomain, port);
}

defaultPort 参数指定要使用的端口,如果输入的字符串没有之一。例如:

The defaultPort parameter specifies the port to use if the input string doesn't have one. For example:

using (NetworkStream s = Connect("google.com", 80).GetStream())
{
    byte[] line = Encoding.UTF8.GetBytes("GET / HTTP/1.0\r\n\r\n");
    s.Write(line, 0, line.Length);

    int b;
    while ((b = s.ReadByte()) != -1)
        Console.Write((char)b);
}

要得C的地址$ C $无需连接到它(例如,以验证它是有效的,还是因为你是通过需要一个IP地址的API连接),此方法将这样做(和可选的执行DNS查询):

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):

public static IPAddress Resolve(string ipAndPort, ref int port, bool resolveDns)
{
    if (ipAndPort.Contains("/"))
        throw new FormatException("Input address should only contain an IP address and port");

    Uri uri = new Uri("tcp://" + ipAndPort);

    if (uri.Port != -1)
        port = uri.Port;
    if (uri.HostNameType == UriHostNameType.IPv4 || uri.HostNameType == UriHostNameType.IPv6)
        return IPAddress.Parse(uri.Host);
    else if (resolveDns)
        return Dns.GetHostAddresses(uri.Host)[0];
    else
        return null;
}

奇怪的是, Dns.GetHostAddresses 可以返回多个地址。我asked它的,显然这是可以简单地采取的第一个地址。

Curiously, Dns.GetHostAddresses can return multiple addresses. I asked about it, and apparently it's okay to simply take the first address.

如果有语法错误或问题将域名解析(出现FormatException 或 SocketException )。如果用户指定的域名,但 resolveDns ==假,此方法返回

An exception will be raised if there is a syntax error or a problem resolving the domain name (FormatException or SocketException). If the user specified a domain name but resolveDns==false, this method returns null.