예제 #1
0
        /// <summary>
        /// 获取Ip对应的地址
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static string GetIpAddress(string ip)
        {
            IPLocation ipLocation = SearchLocation(ip);

            if (ipLocation == null)
            {
                return("");
            }
            else
            {
                return(string.Format("{0}{1}", ipLocation.Country, ipLocation.Area));
            }
        }
예제 #2
0
        /// <summary>
        /// 读取位置
        /// </summary>
        /// <param name="offset">偏移量</param>
        /// <returns></returns>
        private static IPLocation ReadIPLocation(long offset)
        {
            IPLocation ipLocation = new IPLocation();

            _ipdatefile.Position = offset + 4;

            byte flag = (byte)_ipdatefile.ReadByte();

            if (flag == MODE1)
            {
                long countryOffset = ReadByte3();
                _ipdatefile.Position = countryOffset;
                flag = (byte)_ipdatefile.ReadByte();
                if (flag == MODE2)
                {
                    ipLocation.Country   = ReadString(ReadByte3());
                    _ipdatefile.Position = countryOffset + 4;
                }
                else
                {
                    ipLocation.Country = ReadString(countryOffset);
                }
                ipLocation.Area = ReadArea(_ipdatefile.Position);
            }
            else if (flag == MODE2)
            {
                ipLocation.Country = ReadString(ReadByte3());
                ipLocation.Area    = ReadArea(offset + 8);
            }
            else
            {
                ipLocation.Country = ReadString(--_ipdatefile.Position);
                ipLocation.Area    = ReadString(_ipdatefile.Position);
            }
            return(ipLocation);
        }
예제 #3
0
        /// <summary>
        /// 读取位置
        /// </summary>
        /// <param name="offset">偏移量</param>
        /// <returns></returns>
        private static IPLocation ReadIPLocation(long offset) {
            IPLocation ipLocation = new IPLocation();

            _ipdatefile.Position = offset + 4;

            byte flag = (byte)_ipdatefile.ReadByte();
            if (flag == MODE1) {
                long countryOffset = ReadByte3();
                _ipdatefile.Position = countryOffset;
                flag = (byte)_ipdatefile.ReadByte();
                if (flag == MODE2) {
                    ipLocation.Country = ReadString(ReadByte3());
                    _ipdatefile.Position = countryOffset + 4;
                }
                else {
                    ipLocation.Country = ReadString(countryOffset);
                }
                ipLocation.Area = ReadArea(_ipdatefile.Position);
            }
            else if (flag == MODE2) {
                ipLocation.Country = ReadString(ReadByte3());
                ipLocation.Area = ReadArea(offset + 8);
            }
            else {
                ipLocation.Country = ReadString(--_ipdatefile.Position);
                ipLocation.Area = ReadString(_ipdatefile.Position);
            }
            return ipLocation;
        }