4

我是新手。我在 C# 4.0 中编写了一个获取 IP 范围的程序,它适用于小范围,但是当我使用 A 类 IP 地址等范围时,我的程序需要很多时间。需要一些帮助。这是我的代码(这不是一个好方法)。有什么建议以更好的方式写作。?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ipSplitt
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            string[] z = new string[4];
            int t = 0;
            string ip = "3.0.0.0";

            string[] Ip = ip.Split(new char[] { '.' });

            foreach (string m in Ip)
            {
                z[t] = m;
                t++;
            } 

            int a1, b1, c1, d1 = 0;

            a1 = Convert.ToInt32(z[0]);
            b1 = Convert.ToInt32(z[1]);
            c1 = Convert.ToInt32(z[2]);
            d1 = Convert.ToInt32(z[3]);

            string[] v = new string[4];
            string ip2 = "3.255.255.255";
            int l = 0;
            string[] Ip2 = ip2.Split(new char[] { '.' });

            foreach (string m in Ip2)
            {
                v[l] = m;
                l++;
            } 

            int a2, b2, c2, d2 = 0;

            a2 = Convert.ToInt32(v[0]);
            b2 = Convert.ToInt32(v[1]);
            c2 = Convert.ToInt32(v[2]);
            d2 = Convert.ToInt32(v[3]);

            while (d2 >= d1 || c2 > c1 || b2 > b1 || a2 > a1)
            {
                if (d1 > 255)
                {
                    d1 = 1;
                    c1++;
                }

                if (c1 > 255)
                {
                    c1 = 1;
                    b1++;
                }

                if (b1 > 255)
                {
                    b1 = 1;
                    a1++;
                }

                using (StreamWriter writer = new StreamWriter("import.txt",true))
                    writer.WriteLine(a1 + "." + b1 + "." + c1 + "." + d1);

                d1++;
            }
        }
    }
}
4

7 回答 7

5

但是当我选择 A 类 IP 地址等范围时,我的程序会花费很多时间

因为您打开和关闭文件句柄 255^3 次。将using(Streamwriter ...)块放在外部 while 循环周围。

于 2012-04-27T08:07:47.710 回答
1

您的方法很简单,这里已经有了很好的答案。但是... 什么是 IP 地址?它是一个四字节整数,对吧?那么为什么不将范围限制解析为 int32,然后使用掩码在输出流之间打印数字以获得十进制表示:

127.0.0.1 = 01111111 00000000 00000000 00000001
a = ip & 0xFF000000
b = ip & 0x00FF0000 
c = ip & 0x0000FF00
d = ip & 0x000000FF

但仍然不要忘记初始化一个循环外的流。

于 2012-04-27T08:25:07.253 回答
1

StringBuilder 解决方案在我的系统上抛出内存不足异常,因此您只需添加

 StreamWriter writer = new StreamWriter("import.txt");

在你的功能之上然后替换

 using (StreamWriter writer = new StreamWriter("import.txt",true))
            writer.WriteLine(a1 + "." + b1 + "." + c1 + "." + d1);

writer.WriteLine(a1 + "." + b1 + "." + c1 + "." + d1);

并添加

writer.Close();

至底部。10秒内完成。

于 2012-04-27T08:21:19.023 回答
1

很多时间去这个

using (StreamWriter writer = new StreamWriter("import.txt",true))
                writer.WriteLine(a1 + "." + b1 + "." + c1 + "." + d1);

所以你应该这样做

        StringBuilder sb = new StringBuilder(Int32.MaxValue);
        sb.Append(a1.ToString() + "." + b1.ToString() + "." + c1.ToString() + "." + d1.ToString() + "\n");

毕竟只是打电话

        File.WriteAllLines("import.txt", sb.ToString().Split('\n'));
于 2012-04-27T08:10:13.533 回答
1

看看.NET 框架中的IPAddress 类,以及静态 Parse函数。

于 2012-04-27T08:06:22.910 回答
0

写入文件 import.txt 的语句是导致循环变慢的罪魁祸首。我认为您应该将 StreamWriter 代码替换为 ips.AppendLine(a1 + "." + b1 + "." + c1 + "." + d1); Here, ips is a StringBuilder object。在循环之外,我编写了以下代码:

StreamWriter writer = new StreamWriter("import.txt", false))
char[]  arr ;

arr = new char[ips.Length]  ;
ips.CopyTo(0, arr, 0, ips.Length) ;

writer.Write(arr);
writer.Dispose()

在这里,我不会将字符串从 StringBuilder 转储到 import.txt。但我复制到 char[] 然后转储。我观察到这要快得多。我希望这有帮助 !!

于 2012-04-27T09:06:27.027 回答
0

您每次都打开文件,以便为每个 IP 刷新它。将 using 语句放在 while 块周围。

于 2012-04-27T08:07:43.880 回答