簡體   English   中英

從字符串創建 IPEndpoint 的最佳方法

[英]Best way to create IPEndpoint from string

由於IPEndpoint包含一個ToString()方法,該方法輸出:

10.10.10.10:1010

還應該有Parse()和/或TryParse()方法,但沒有。

我可以在:上拆分字符串並解析 IP 地址和端口。

但是有沒有更優雅的方式呢?

這是一種解決方案...

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);
}

我需要使用 IPv6、v4 和主機名解析 IPEndpoint。 我寫的解決方案如下:

    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
  • 例子.org
  • 例如.org:100

看起來已經有一個內置的 Parse 方法來處理 ip4 和 ip6 地址http://msdn.microsoft.com/en-us/library/system.net.ipaddress.parse%28v=vs.110%29.aspx

// serverIP can be in ip4 or ip6 format
string serverIP = "192.168.0.1";
int port = 8000;
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(serverIP), port);

這是我將文本解析為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");
}

測試:

顯然, IPEndPoint.ParseIPEndPoint.TryParse 是在 .NET Core 3.0 中添加的

如果您的目標是它,請嘗試使用這些方法! 實現見上面的鏈接。

這是一個非常簡單的解決方案,它同時處理 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使用TryParse

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

這將執行 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);
}

這是我對 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;
    }

創建擴展方法 Parse 和 TryParse。 我想那更優雅。

IPv4 端點的解析代碼很簡單,但 IPv6 地址上的 IPEndPoint.ToString() 也使用相同的冒號表示法,但與 IPv6 地址的冒號表示法沖突。 我希望微軟會花精力寫這個丑陋的解析代碼,但我想我必須……

如果端口號總是在':'之后提供,那么下面的方法可能是一個更優雅的選擇(代碼長度而不是效率)。

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 地址格式。

.NET 3 代碼(對於 .NET 4.7)的粗略轉換是這樣的:

    // ReSharper disable once InconsistentNaming
    public static class IPEndPointExtensions
    {
        public static bool TryParse(string s, out IPEndPoint result)
        {
            int addressLength = s.Length;  // If there's no port then send the entire string to the address parser
            int lastColonPos = s.LastIndexOf(':');

            // Look to see if this is an IPv6 address with a port.
            if (lastColonPos > 0)
            {
                if (s[lastColonPos - 1] == ']')
                {
                    addressLength = lastColonPos;
                }
                // Look to see if this is IPv4 with a port (IPv6 will have another colon)
                else if (s.Substring(0, lastColonPos).LastIndexOf(':') == -1)
                {
                    addressLength = lastColonPos;
                }
            }

            if (IPAddress.TryParse(s.Substring(0, addressLength), out IPAddress address))
            {
                uint port = 0;

                if (addressLength == s.Length ||
                    (uint.TryParse(s.Substring(addressLength + 1), NumberStyles.None, CultureInfo.InvariantCulture, out port) && port <= IPEndPoint.MaxPort))

                {
                    result = new IPEndPoint(address, (int)port);

                    return true;
                }
            }

            result = null;

            return false;
        }

        public static IPEndPoint Parse(string s)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }

            if (TryParse(s, out IPEndPoint result))
            {
                return result;
            }

            throw new FormatException(@"An invalid IPEndPoint was specified.");
        }
    }
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" : $@"IPEndPoint {{
  Address: {point.Address}
  AddressFamily: {point.AddressFamily}
  Port: {point.Port}
}}";
      Console.WriteLine($@"""{sample}"" to IPEndPoint is {report}
");
    }
  }
}
IPAddress ipAddress = IPAddress.Parse(yourIPAddress);
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Convert.ToInt16(yourPortAddress));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM