首页 > 代码库 > C# 随机获取国内IP

C# 随机获取国内IP

调用getRandomIp()方法即可
Framework3.5 +使用LINQ

public string getRandomIp()
        {     /*
     int[][]
     这个叫交错数组,白话文就是数组的数组.
    初始化的方法:
     int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
当然也可以使用{}初始化器初始化
             int[][] numbers = { new int[] {2,3,4}, 
                            new int[] {5,6,7,8,9} 
                          };
     */
         int[][] range = {new int[]{607649792,608174079},//36.56.0.0-36.63.255.255
new int[]{1038614528,1039007743},//61.232.0.0-61.237.255.255
new int[]{1783627776,1784676351},//106.80.0.0-106.95.255.255
new int[]{2035023872,2035154943},//121.76.0.0-121.77.255.255
new int[]{2078801920,2079064063},//123.232.0.0-123.235.255.255
new int[]{-1950089216,-1948778497},//139.196.0.0-139.215.255.255
new int[]{-1425539072,-1425014785},//171.8.0.0-171.15.255.255
new int[]{-1236271104,-1235419137},//182.80.0.0-182.92.255.255
new int[]{-770113536,-768606209},//210.25.0.0-210.47.255.255
 new int[]{-569376768,-564133889}, //222.16.0.0-222.95.255.255
};
 
            Random rdint = new Random();
            int index = rdint.Next(10);
            string ip = num2ip(range[index][0] + new Random().Next(range[index][1] - range[index][0]));
            return ip;
        }
 
        /*
         * 将十进制转换成ip地址
        */
        public string num2ip(int ip)
        {
            int[] b = new int[4];
            string x = "";
            //位移然后与255 做高低位转换
            b[0] = (int)((ip >> 24) & 0xff);    
            b[1] = (int)((ip >> 16) & 0xff);
            b[2] = (int)((ip >> 8) & 0xff);
            b[3] = (int)(ip & 0xff);
            x = (b[0]).ToString() + "." + (b[1]).ToString() + "." + (b[2]).ToString() + "." + (b[3]).ToString();
 
            return x;
        }

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    class Program
    {
        static Dictionary<int, int> ips = new int[][]
            {
                Enumerable.Range(607649792, 608174079 - 607649792 + 1).ToArray(),
                Enumerable.Range(1038614528, 1039007743 - 1038614528 + 1).ToArray(),
                Enumerable.Range(1783627776, 1784676351 - 1783627776 + 1).ToArray(),
                Enumerable.Range(2035023872, 2035154943 - 2035023872 + 1).ToArray(),
                Enumerable.Range(2078801920, 2079064063 - 2078801920 + 1).ToArray(),
                Enumerable.Range(-1950089216, -1948778497 + 1950089216 + 1).ToArray(),
                Enumerable.Range(-1425539072, -1425014785 + 1425539072 + 1).ToArray(),
                Enumerable.Range(-1236271104, -1235419137 + 1236271104 + 1).ToArray(),
                Enumerable.Range(-770113536, -768606209 + 770113536 + 1).ToArray(),
                Enumerable.Range(-569376768, -564133889 + 569376768 + 1).ToArray()
            }.SelectMany(x => x).Select((x, i) => new { x, i }).ToDictionary(x => x.i, x => x.x);
 
        static string IpToString(int IP)
        {
            return string.Join(".", BitConverter.GetBytes(IP).Reverse().Select(x => ((int)x).ToString()));
        }
 
        static int GenIP()
        {
            return ips[new Random(Guid.NewGuid().GetHashCode()).Next(0, ips.Count)];
        }
 
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
                Console.WriteLine(IpToString(GenIP()));
        }
    }
}

加了比较不容易理解部分的注释..想弄明白的同学想必现在已经明白了吧

C# 随机获取国内IP