Пример #1
0
        /// <summary>
        /// Get the port from the given URI...must be well formed
        /// </summary>
        /// <param name="uri">The URI string</param>
        /// <returns>int</returns>
        public static int GetUriPort(string uri)
        {
            if (uri == null || uri.Trim().Length == 0)
            {
                return(0);
            }

            int    startIdx   = uri.IndexOf("://") + 3;
            int    idxOfColon = uri.IndexOf(":", startIdx);
            int    idxOfSlash = -1;
            string port       = "";

            if (idxOfColon < 0)
            {
                return(AbstractNetworkUtils.GetDefaultCoAPPort());
            }
            else
            {
                idxOfSlash = uri.IndexOfAny(new char[] { '\\', '/' }, idxOfColon);
                if (idxOfSlash != -1)
                {
                    port = uri.Substring(idxOfColon + 1, idxOfSlash - idxOfColon - 1);
                }
                else
                {
                    port = uri.Substring(idxOfColon + 1);
                }
                return(int.Parse(port));
            }
        }
Пример #2
0
        /// <summary>
        /// Get the port from the given URI...must be well formed
        /// </summary>
        /// <param name="uri">The URI string</param>
        /// <returns>int</returns>
        public static int GetUriPort(string uri)
        {
            if (uri == null || uri.Trim().Length == 0)
            {
                return(0);
            }

            int startIdx = -1;

            // If the URI contains square brackets, it is IPv6 as per RFC3986 http://www.ietf.org/rfc/rfc3986.txt
            if (uri.Contains("[") && uri.Contains("]"))
            {
                startIdx = uri.IndexOf("]") + 1;
            }
            else
            {
                startIdx = uri.IndexOf("://") + 3;
            }

            int    idxOfColon = uri.IndexOf(":", startIdx);
            int    idxOfSlash = -1;
            string port       = "";

            if (idxOfColon < 0)
            {
                return(AbstractNetworkUtils.GetDefaultCoAPPort());
            }
            else
            {
                idxOfSlash = uri.IndexOfAny(new char[] { '\\', '/' }, idxOfColon);
                if (idxOfSlash != -1)
                {
                    port = uri.Substring(idxOfColon + 1, idxOfSlash - idxOfColon - 1);
                }
                else
                {
                    port = uri.Substring(idxOfColon + 1);
                }
                return(int.Parse(port));
            }
        }
Пример #3
0
        /// <summary>
        /// Get the port from the given URI...must be well formed
        /// </summary>
        /// <param name="uri">The URI string</param>
        /// <returns>int</returns>
        public static int GetUriPort(string uri, System.Net.Sockets.AddressFamily addrFamily)
        {
            if (uri == null || uri.Trim().Length == 0)
            {
                return(0);
            }

            int startIdx   = uri.IndexOf("://") + 3;
            int idxOfColon = -1;

            if (addrFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                idxOfColon = uri.IndexOf(":", startIdx);
            }
            else
            {
                idxOfColon = uri.LastIndexOf(":");
            }
            int    idxOfSlash = -1;
            string port       = "";

            if (idxOfColon < 0)
            {
                return(AbstractNetworkUtils.GetDefaultCoAPPort());
            }
            else
            {
                idxOfSlash = uri.IndexOfAny(new char[] { '\\', '/' }, idxOfColon);
                if (idxOfSlash != -1)
                {
                    port = uri.Substring(idxOfColon + 1, idxOfSlash - idxOfColon - 1);
                }
                else
                {
                    port = uri.Substring(idxOfColon + 1);
                }
            }

            return(int.Parse(port));
        }