【问题标题】:Best way to create IPEndpoint from string从字符串创建 IPEndpoint 的最佳方法
【发布时间】:2011-02-13 05:33:34
【问题描述】:

由于IPEndpoint 包含一个ToString() 方法,该方法输出:

10.10.10.10:1010

还应该有 Parse() 和/或 TryParse() 方法,但没有。

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

但是还有更优雅的方式吗?

【问题讨论】:

    标签: c# ip-address


    【解决方案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);
    }
    

    【讨论】:

    • 这或多或少是我在问题中暗示的解决方案?但做得很好。
    • @S_Hultqvist,我知道,我第一次阅读你的问题时错过了那部分。直到我发布答案后我才注意到它。
    • 不好:CreateIPEndPoint(new IPEndPoint(IPAddress.Parse("2001:740:8deb:0::1"), 5222).ToString())
    • @Nick,感谢您指出这一点,我添加了一种适用于 IPv4 和 IPv6 的新方法。
    【解决方案2】:

    我需要使用 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
    • example.org
    • example.org:100

    【讨论】:

    • "[a:b:c:d]" 失败。
    • @JasperCiti,我无法重现该问题。如果我调用EndpointParser.Parse("[1:2:3::4]"),我会正确收到异常未指定端口:'[1:2:3::4]'。如果我打电话给EndpointParser.Parse("[1:2:3::4]", 80),我会得到解析后的IPEndPoint for [1:2:3::4]:80
    • 也许我误解了你的测试用例,但我的印象是你在测试用例中传递的字符串与 "[a:b:c:d]" 完全一样,它应该以某种方式解决一些IPEndPoint。虽然不是火车粉碎。谢谢你的算法。我基于您的版本构建了自己的版本,唯一需要更改的是删除 Linq 依赖项,因为在移动应用程序上拥有一个精简客户端对我来说非常重要。
    【解决方案3】:

    看起来已经有一个内置的 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);
    

    【讨论】:

    • 这不包括像localhost:port这样的字符串中的主机和端口。
    • @BeachMiles 不应该 string port int port?
    【解决方案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】:

      显然,IPEndPoint.ParseIPEndPoint.TryParse were added in .NET Core 3.0

      如果您要针对它,请尝试这些方法!实现见上面的链接。

      【讨论】:

      • 在 NET 4.8 中不受支持,有什么建议吗?
      • @Reza 复制链接源或查看其他答案。
      【解决方案6】:

      这是一个非常简单的解决方案,它同时处理 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);
      

      【讨论】:

        【解决方案7】:

        这将执行 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);
        }
        

        【讨论】:

        • 你的意思可能是if (2 &lt; values.Length)
        【解决方案8】:

        创建扩展方法 Parse 和 TryParse。我想这更优雅。

        【讨论】:

          【解决方案9】:

          解析代码对于 IPv4 端点很简单,但 IPv6 地址上的 IPEndPoint.ToString() 也使用相同的冒号表示法,但与 IPv6 地址的冒号表示法冲突。我希望微软会花精力编写这个丑陋的解析代码,但我想我必须......

          【讨论】:

            【解决方案10】:

            这是我对 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;
                }
            

            【讨论】:

              【解决方案11】:

              如果总是在':' 之后提供端口号,则以下方法可能是更优雅的选择(在代码长度而不是效率方面)。

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

              【讨论】:

                【解决方案12】:

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

                【讨论】:

                  【解决方案13】:
                  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}
                  ");
                      }
                    }
                  }
                  

                  【讨论】:

                    【解决方案14】:
                    IPAddress ipAddress = IPAddress.Parse(yourIPAddress);
                    IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Convert.ToInt16(yourPortAddress));
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2011-04-28
                      • 1970-01-01
                      • 1970-01-01
                      • 2011-06-02
                      • 1970-01-01
                      • 1970-01-01
                      • 2016-01-24
                      • 2011-12-15
                      相关资源
                      最近更新 更多