2012-04-27 58 views
4

我是新手。我在C#4.0中編寫了一個獲取IP範圍的程序,它適用於小範圍,但是當我使用類A IP地址等範圍時,我的程序需要大量時間,需要一些幫助。是我的代碼(它不是一個好方法)。任何建議以更好的方式寫。如何在C#中獲取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++; 
      } 
     } 
    } 
} 

回答

1

看一看在.NET框架IPAddress classstatic Parse功能。

+0

這不是OP正在做的事情。他不解析它們,而是打印。 – CodeCaster 2012-04-27 08:09:19

+0

我可能已經明白他的問題了。 PS。 +1爲您的答案。在我寫回答之後才注意到它。但是當我想編輯你已經回答了它。 – SynerCoder 2012-04-27 08:10:50

0

您每次打開文件時都會沖洗每個IP。將use語句放在while塊中。

5

但是當我去像A類IP地址範圍,我的程序需要花費大量的時間

因爲你打開和關閉文件句柄255^3倍。將using(Streamwriter ...)塊放在外部while循環周圍。

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')); 
+0

在我的系統中拋出內存不足異常 – accfews 2012-04-27 08:21:40

+0

在投入downvote之前嘗試執行此操作StringBuilder(Int32,Int32) – Likurg 2012-04-27 08:31:21

+0

accfews嘗試編輯變體 – Likurg 2012-04-27 08:34:28

1

StringBuilder的解決方案拋出內存異常的我係統,所以你應該簡單地添加

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

你的函數的頂部,然後用

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

更換

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

,並添加

writer.Close(); 

底部。在10秒內完成。

+0

試試我的編輯請 – Likurg 2012-04-27 08:36:40

1

你的方法很簡單,這裏已經有很好的答案。但是...什麼是IP地址?它是一個四字節的整數,對嗎?那麼,爲什麼不來解析範圍限制爲INT32,然後打印之間的數字輸出流與使用面膜來獲得十進制表示:

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

不過還是不要忘了初始化流退出循環。

0

寫入文件import.txt的語句是導致循環減慢的主要罪魁禍首。我認爲你應該用ips.AppendLine(a1 + "." + b1 + "." + c1 + "." + d1);替換StreamWriter代碼在這裏,ips是一個StringBuilder對象。 外循環,我寫了下面的代碼:

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 []然後轉儲。我觀察到這個速度很快。我希望這有幫助 !!