Example #1
0
        public static string ConvertIpAddress(Java.Net.InetAddress inetAddress)
        {
            if (inetAddress == null)
            {
                return(null);
            }

            try
            {
                string address = inetAddress.HostAddress;
                if (address == null)
                {
                    string text = inetAddress.ToString();
                    System.Text.RegularExpressions.Match match =
                        System.Text.RegularExpressions.Regex.Match(text, @"\d+\.\d+\.\d+\.\d+$", System.Text.RegularExpressions.RegexOptions.Singleline);
                    if (match.Success)
                    {
                        address = match.Value;
                    }
                }
                return(address);
            }
            catch (Exception)
            {
                return(null);
            }
        }
 public static string ConvertIpAddress(int ipAddress)
 {
     if (Java.Nio.ByteOrder.NativeOrder().Equals(Java.Nio.ByteOrder.LittleEndian))
     {
         ipAddress = Java.Lang.Integer.ReverseBytes(ipAddress);
     }
     byte[] ipByteArray = Java.Math.BigInteger.ValueOf(ipAddress).ToByteArray();
     try
     {
         Java.Net.InetAddress inetAddress = Java.Net.InetAddress.GetByAddress(ipByteArray);
         string address = inetAddress.HostAddress;
         if (address == null)
         {
             string text = inetAddress.ToString();
             System.Text.RegularExpressions.Match match =
                 System.Text.RegularExpressions.Regex.Match(text, @"\d+\.\d+\.\d+\.\d+$", System.Text.RegularExpressions.RegexOptions.Singleline);
             if (match.Success)
             {
                 address = match.Value;
             }
         }
         return(address);
     }
     catch (Exception)
     {
         return(null);
     }
 }
Example #3
0
        /// <summary>
        /// Convert Java InetAddress to .Net IPAddress.
        /// </summary>
        /// <param name="inetAddress">The InetAddress object.</param>
        /// <returns>The equivalent .Net IPAddress object.</returns>
        /// <exception cref="System.ArgumentException">Failed to parse InetAddress: ...</exception>
        public static System.Net.IPAddress ToIPAddress(this Java.Net.InetAddress inetAddress)
        {
            if (inetAddress == null)
            {
                return(null);
            }

            // Use get the bytes and create an IP address from that
            if (inetAddress.GetAddress() != null && inetAddress.GetAddress().Length > 0)
            {
                return(new System.Net.IPAddress(inetAddress.GetAddress()));
            }

            // Use the host name string which should be in the standard "1.2.3.4" format
            System.Net.IPAddress ipAddress;
            if (System.Net.IPAddress.TryParse(inetAddress.HostAddress, out ipAddress))
            {
                return(ipAddress);
            }

            // In Android 7 (Nougat) both of the above return null for IPv4 addresses however ToString still appears to work

            // The IP address should be the bit following the slash
            string str = inetAddress.ToString().Split('/').LastOrDefault();

            if (System.Net.IPAddress.TryParse(str, out ipAddress))
            {
                return(ipAddress);
            }

            // Don't know what else to try
            throw new ArgumentException(string.Format("Failed to parse InetAddress: {0}", inetAddress.ToString()));
        }