zoukankan      html  css  js  c++  java
  • IP地址段遍历

    #region 搜索ftp服务器地址
            /// <summary>
            /// 搜索ftp服务器
            /// </summary>
            public void SearchFtpServer()
            {
     
    var beginIp=""192.168.1.1;
    var endIp=""192.168.1.251;
                var currentIp = IPAddress.Parse(beginIp);
                var endIp = IPAddress.Parse(endIp);
     
                var beginNum = GetIpNumber(currentIp);
                var endNum = GetIpNumber(endIp);
     
                for (long i = beginNum; i <= endNum; i++)
                {
                  //TODO:根据ip地址做事
                    currentIp = NextIP(currentIp);
                }
            }
     
            #region ip地址相关
            /// <summary>
            /// 将ip地址转换为数字表示
            /// </summary>
            /// <param name="ip"></param>
            /// <returns></returns>
            private long GetIpNumber(IPAddress ip)
            {
                var ipBytes = ip.ToString().Split(new char[] { '.' });
                var num = Convert.ToInt64(ipBytes[0]) * 256 * 256 * 256 +
                    Convert.ToInt64(ipBytes[1]) * 256 * 256 +
                    Convert.ToInt64(ipBytes[2]) * 256 +
                    Convert.ToInt64(ipBytes[3]);
                return num;
            }
     
            /// <summary>
            /// 从一个IPAddress 获取它的下一个 IPAddress
            /// </summary>
            /// <param name="theIP"></param>
            /// <returns></returns>
            private IPAddress NextIP(IPAddress theIP)
            {
                byte[] ipBytes = theIP.GetAddressBytes();
                if (ipBytes[3] != 255)
                {
                    ipBytes[3] += 1;
                }
                else if (ipBytes[2] != 255)
                {
                    ipBytes[2] += 1;
                    ipBytes[3] = 1;
                }
                else if (ipBytes[1] != 255)
                {
                    ipBytes[1] += 1;
                    ipBytes[2] = 1;
                    ipBytes[3] = 1;
                }
                else if (ipBytes[0] != 255)
                {
                    ipBytes[0] += 1;
                    ipBytes[1] = 1;
                    ipBytes[2] = 1;
                    ipBytes[3] = 1;
                }
                IPAddress ip = new IPAddress(ipBytes);
     
                return ip;
            }
            #endregion
  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/maomao999/p/3668400.html
Copyright © 2011-2022 走看看