Подтвердить что ты не робот

Лучший способ создать IPEndpoint из строки

Так как IPEndpoint содержит метод ToString(), который выводит:

10.10.10.10:1010

Также должен быть метод Parse() и/или TryParse(), но его нет.

Я могу разделить строку на : и проанализировать IP-адрес и порт.

Но есть ли более элегантный способ?

4b9b3361

Ответ 1

Это одно решение...

public static IPEndPoint CreateIPEndPoint(string endPoint)
{
    string[] ep = endPoint.Split(':');
    if(ep.Length != 2) throw new FormatException("Invalid endpoint format");
    IPAddress ip;
    if(!IPAddress.TryParse(ep[0], out ip))
    {
        throw new FormatException("Invalid ip-adress");
    }
    int port;
    if(!int.TryParse(ep[1], NumberStyles.None, NumberFormatInfo.CurrentInfo, out port))
    {
        throw new FormatException("Invalid port");
    }
    return new IPEndPoint(ip, port);
}

Изменить: добавлена ​​версия, которая будет обрабатывать IPv4 и IPv6, предыдущий обрабатывает только IPv4.

// Handles IPv4 and IPv6 notation.
public static IPEndPoint CreateIPEndPoint(string endPoint)
{
    string[] ep = endPoint.Split(':');
    if (ep.Length < 2) throw new FormatException("Invalid endpoint format");
    IPAddress ip;
    if (ep.Length > 2)
    {
        if (!IPAddress.TryParse(string.Join(":", ep, 0, ep.Length - 1), out ip))
        {
            throw new FormatException("Invalid ip-adress");
        }
    }
    else
    {
        if (!IPAddress.TryParse(ep[0], out ip))
        {
            throw new FormatException("Invalid ip-adress");
        }
    }
    int port;
    if (!int.TryParse(ep[ep.Length - 1], NumberStyles.None, NumberFormatInfo.CurrentInfo, out port))
    {
        throw new FormatException("Invalid port");
    }
    return new IPEndPoint(ip, port);
}

Ответ 2

У меня было требование разбора IPEndpoint с IPv6, v4 и именами хостов. Решение, которое я написал, приведено ниже:

    public static IPEndPoint Parse(string endpointstring)
    {
        return Parse(endpointstring, -1);
    }

    public static IPEndPoint Parse(string endpointstring, int defaultport)
    {
        if (string.IsNullOrEmpty(endpointstring)
            || endpointstring.Trim().Length == 0)
        {
            throw new ArgumentException("Endpoint descriptor may not be empty.");
        }

        if (defaultport != -1 &&
            (defaultport < IPEndPoint.MinPort
            || defaultport > IPEndPoint.MaxPort))
        {
            throw new ArgumentException(string.Format("Invalid default port '{0}'", defaultport));
        }

        string[] values = endpointstring.Split(new char[] { ':' });
        IPAddress ipaddy;
        int port = -1;

        //check if we have an IPv6 or ports
        if (values.Length <= 2) // ipv4 or hostname
        {
            if (values.Length == 1)
                //no port is specified, default
                port = defaultport;
            else
                port = getPort(values[1]);

            //try to use the address as IPv4, otherwise get hostname
            if (!IPAddress.TryParse(values[0], out ipaddy))
                ipaddy = getIPfromHost(values[0]);
        }
        else if (values.Length > 2) //ipv6
        {
            //could [a:b:c]:d
            if (values[0].StartsWith("[") && values[values.Length - 2].EndsWith("]"))
            {
                string ipaddressstring = string.Join(":", values.Take(values.Length - 1).ToArray());
                ipaddy = IPAddress.Parse(ipaddressstring);
                port = getPort(values[values.Length - 1]);
            }
            else //[a:b:c] or a:b:c
            {
                ipaddy = IPAddress.Parse(endpointstring);
                port = defaultport;
            }
        }
        else
        {
            throw new FormatException(string.Format("Invalid endpoint ipaddress '{0}'", endpointstring));
        }

        if (port == -1)
            throw new ArgumentException(string.Format("No port specified: '{0}'", endpointstring));

        return new IPEndPoint(ipaddy, port);
    }

    private static int getPort(string p)
    {
        int port;

        if (!int.TryParse(p, out port)
         || port < IPEndPoint.MinPort
         || port > IPEndPoint.MaxPort)
        {
            throw new FormatException(string.Format("Invalid end point port '{0}'", p));
        }

        return port;
    }

    private static IPAddress getIPfromHost(string p)
    {
        var hosts = Dns.GetHostAddresses(p);

        if (hosts == null || hosts.Length == 0)
            throw new ArgumentException(string.Format("Host not found: {0}", p));

        return hosts[0];
    }

Это было протестировано для работы со следующими примерами:

  • 0.0.0.0:100
  • 0.0.0.0
  • [:: 1]: 100
  • [:: 1]
  • :: 1
  • [A: B: C: D]
  • [A: B: C: D]: 100
  • example.org
  • example.org:100

Ответ 4

Вот моя версия парсинга текста в IPEndPoint:

private static IPEndPoint ParseIPEndPoint(string text)
{
    Uri uri;
    if (Uri.TryCreate(text, UriKind.Absolute, out uri))
        return new IPEndPoint(IPAddress.Parse(uri.Host), uri.Port < 0 ? 0 : uri.Port);
    if (Uri.TryCreate(String.Concat("tcp://", text), UriKind.Absolute, out uri))
        return new IPEndPoint(IPAddress.Parse(uri.Host), uri.Port < 0 ? 0 : uri.Port);
    if (Uri.TryCreate(String.Concat("tcp://", String.Concat("[", text, "]")), UriKind.Absolute, out uri))
        return new IPEndPoint(IPAddress.Parse(uri.Host), uri.Port < 0 ? 0 : uri.Port);
    throw new FormatException("Failed to parse text to IPEndPoint");
}

Протестировано с:

Ответ 5

Это будет IPv4 и IPv6. Метод расширения для этой функции был бы на System.string. Не уверен, что я хочу эту опцию для каждой строки, которая у меня есть в проекте.

private static IPEndPoint IPEndPointParse(string endpointstring)
{
    string[] values = endpointstring.Split(new char[] {':'});

    if (2 > values.Length)
    {
        throw new FormatException("Invalid endpoint format");
    }

    IPAddress ipaddress;
    string ipaddressstring = string.Join(":", values.Take(values.Length - 1).ToArray());
    if (!IPAddress.TryParse(ipaddressstring, out ipaddress))
    {
        throw new FormatException(string.Format("Invalid endpoint ipaddress '{0}'", ipaddressstring));
    }

    int port;
    if (!int.TryParse(values[values.Length - 1], out port)
     || port < IPEndPoint.MinPort
     || port > IPEndPoint.MaxPort)
    {
        throw new FormatException(string.Format("Invalid end point port '{0}'", values[values.Length - 1]));
    }

    return new IPEndPoint(ipaddress, port);
}

Ответ 6

Создайте метод расширения Parse и TryParse. Думаю, это более элегантно.

Ответ 7

Синтаксический код прост для конечной точки IPv4, но IPEndPoint.ToString() на IPv6-адресе также использует одну и ту же запись двоеточия, но конфликтует с нотной двоеточечной записью IPv6. Я надеялся, что Microsoft потратит усилия на то, чтобы написать этот уродливый код синтаксического анализа, но я думаю, мне придется...

Ответ 8

Это мой подход к анализу IPEndPoint. Использование класса Uri позволяет избежать специфики IPv4/6 и наличия или отсутствия порта. Вы можете изменить порт по умолчанию для своего приложения.

    public static bool TryParseEndPoint(string ipPort, out System.Net.IPEndPoint result)
    {
        result = null;

        string scheme = "iiiiiiiiiaigaig";
        GenericUriParserOptions options =
            GenericUriParserOptions.AllowEmptyAuthority |
            GenericUriParserOptions.NoQuery |
            GenericUriParserOptions.NoUserInfo |
            GenericUriParserOptions.NoFragment |
            GenericUriParserOptions.DontCompressPath |
            GenericUriParserOptions.DontConvertPathBackslashes |
            GenericUriParserOptions.DontUnescapePathDotsAndSlashes;
        UriParser.Register(new GenericUriParser(options), scheme, 1337);

        Uri parsedUri;
        if (!Uri.TryCreate(scheme + "://" + ipPort, UriKind.Absolute, out parsedUri))
            return false;
        System.Net.IPAddress parsedIP;
        if (!System.Net.IPAddress.TryParse(parsedUri.Host, out parsedIP))
            return false;

        result = new System.Net.IPEndPoint(parsedIP, parsedUri.Port);
        return true;
    }

Ответ 9

Если номер порта всегда предоставляется после ':', следующий метод может быть более элегантным (в случае длины кода вместо эффективности).

public static IPEndpoint ParseIPEndpoint(string ipEndPoint) {
    int ipAddressLength = ipEndPoint.LastIndexOf(':');
    return new IPEndPoint(
        IPAddress.Parse(ipEndPoint.Substring(0, ipAddressLength)),
        Convert.ToInt32(ipEndPoint.Substring(ipAddressLength + 1)));
}

Он отлично работает для моего простого приложения без учета сложного формата IP-адреса.

Ответ 10

Вот очень простое решение, оно обрабатывает как IPv4, так и IPv6.

public class IPEndPoint : System.Net.IPEndPoint
{
    public IPEndPoint(long address, int port) : base(address, port) { }
    public IPEndPoint(IPAddress address, int port) : base(address, port) { }

    public static bool TryParse(string value, out IPEndPoint result)
    {
        if (!Uri.TryCreate($"tcp://{value}", UriKind.Absolute, out Uri uri) ||
            !IPAddress.TryParse(uri.Host, out IPAddress ipAddress) ||
            uri.Port < 0 || uri.Port > 65535)
        {
            result = default(IPEndPoint);
            return false;
        }

        result = new IPEndPoint(ipAddress, uri.Port);
        return true;
    }
}

Просто используйте TryParse как обычно.

IPEndPoint.TryParse("192.168.1.10:80", out IPEndPoint ipv4Result);
IPEndPoint.TryParse("[fd00::]:8080", out IPEndPoint ipv6Result);

Ответ 11

По-видимому, IPEndPoint.Parse и IPEndPoint.TryParse были недавно добавлены в .NET Core 3.0.

Если вы нацелены на это, попробуйте эти методы! Реализация видна по ссылке выше.

Ответ 12

using System;
using System.Net;

static class Helper {
  public static IPEndPoint ToIPEndPoint(this string value, int port = IPEndPoint.MinPort) {
    if (string.IsNullOrEmpty(value) || ! IPAddress.TryParse(value, out var address)) 
      return null;
    var offset = (value = value.Replace(address.ToString(), string.Empty)).LastIndexOf(':');
    if (offset >= 0)
      if (! int.TryParse(value.Substring(offset + 1), out port) || port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
        return null;
    return new IPEndPoint(address, port);
  }
}

class Program {
  static void Main() {
    foreach (var sample in new [] {
      // See https://docops.ca.com/ca-data-protection-15/en/implementing/platform-deployment/technical-information/ipv6-address-and-port-formats
      "192.168.0.3",
      "fe80::214:c2ff:fec8:c920",
      "10.0.1.53-10.0.1.80",
      "10.0",
      "10/7",
      "2001:0db8:85a3/48",
      "192.168.0.5:10",
      "[fe80::e828:209d:20e:c0ae]:375",
      ":137-139",
      "192.168:1024-65535",
      "[fe80::]-[fe81::]:80"
    }) {
      var point = sample.ToIPEndPoint();
      var report = point == null ? "NULL" : [email protected]"IPEndPoint {{
  Address: {point.Address}
  AddressFamily: {point.AddressFamily}
  Port: {point.Port}
}}";
        Console.WriteLine([email protected]"""{sample}"" to IPEndPoint is {report}
");
    }
  }
}