2012-01-18 4 views
0

私はIPアドレスの範囲をpingできるコードを作成しようとしています。この構文はIP範囲をpingするのに使用できますか?

2つの入力があります。 1つは範囲を開始するアドレスで、もう1つはpingしたいIP範囲の終わりです。次に、文字列を分割し、それぞれの値を変更する変数(a、b、c、d、aa、bb、cc、dd)に割り当てます。

これは論理的に正しいですか?

これは私のコードです:

public PingIPRange() 
{ 
    InitializeComponent(); 

    txtFrom.Text = "250.250.250.250"; 
    txtTo.Text = "254.254.224.254"; 

    string[] from = txtFrom.Text.Split('.'); 
    string[] to = txtTo.Text.Split('.'); 

    int from1 = a = int.Parse(from[0]); 
    int from2 = b = int.Parse(from[1]); 
    int from3 = c = int.Parse(from[2]); 
    int from4 = d = int.Parse(from[3]); 

    int to1 = aa = int.Parse(to[0]); 
    int to2 = bb = int.Parse(to[1]); 
    int to3 = cc = int.Parse(to[2]); 
    int to4 = dd = int.Parse(to[3]); 

    tmrPingInterval.Tick += new EventHandler(tmrPingInterval_Tick); 
} 

void tmrPingInterval_Tick(object sender, EventArgs e) 
{ 
    if (d <= max) 
    { 
     if (d == max || d == dd) 
     { 
      c++; 
      d = 0; 
     } 
     if (c == max || c == cc) 
     { 
      d++; 
      c = 0; 
     } 
     if (b == max || b == bb) 
     { 
      c++; 
      b = 0; 
     } 
     if (a == max || a == aa) 
     { 
      b++; 
      a = 0; 
     } 

     if ((a == max && b == max && c == max && d == max) || (a == aa && b == bb && c == cc && d == dd)) 
     { 
      tmrPingInterval.Stop(); 
     } 

     txtDisplay.Text += a + "." + b + "." + c + "." + d + Environment.NewLine; 

     d++; 
    } 

    txtDisplay.SelectionStart = txtDisplay.Text.Length; 
    txtDisplay.ScrollToCaret(); 
} 
+0

技術的にはあなたが変数に、B、C、D、AA、BB、CCを宣言することはありませんので、ノー、ddは –

+0

Iを私のコードを更新しました。 – HelpNeeder

答えて

2

あなたはそれを過度に複雑です!ここでは一例です

static uint str2ip(string ip) 
{ 
    string[] numbers = ip.Split('.'); 

    uint x1 = (uint)(Convert.ToByte(numbers[0]) << 24); 
    uint x2 = (uint)(Convert.ToByte(numbers[1]) << 16); 
    uint x3 = (uint)(Convert.ToByte(numbers[2]) << 8); 
    uint x4 = (uint)(Convert.ToByte(numbers[3])); 

    return x1 + x2 + x3 + x4; 
} 

static string ip2str(uint ip) 
{ 
    string s1 = ((ip & 0xff000000) >> 24).ToString() + "."; 
    string s2 = ((ip & 0x00ff0000) >> 16).ToString() + "."; 
    string s3 = ((ip & 0x0000ff00) >> 8).ToString() + "."; 
    string s4 = (ip & 0x000000ff).ToString(); 

    string ip2 = s1 + s2 + s3 + s4; 
    return ip2; 
} 

static void Main(string[] args) 
{ 
    uint startIP = str2ip("250.255.255.100"); 
    uint endIP = str2ip("255.0.1.255"); 

    for(uint currentIP = startIP; currentIP <= endIP; currentIP++) { 
     string thisIP = ip2str(currentIP); 
     Console.WriteLine(thisIP); 
    } 

    Console.ReadKey(); 
} 
+0

これは例としてうまく見えます。あなたのコードを使用しようとすると、私はフィードバックをします。 – HelpNeeder

+0

賢いですが、私はこれを単純とは言いません。 4バイトを符号なし整数にビットシフトして符号化するという概念は、多くのプログラマにとって奇妙なことです。 –

+0

@Igby Largeman、私は一種同意します。しかし、コンセプトはラップアラウンドしやすいです。ここで唯一困難な部分は、uintへの変換、そしてその逆です。 – HelpNeeder

1

私はあなたがクラスIPAddressを使用することをお勧めします私はこのようなものでそれをシンプルに保つと思います。 あなたは、このコード使用してIPアドレスを反復処理できるより:

IPAddress ipAddress = IPAddress.Parse(txtFrom.Text); 

byte[] bytes = ipAddress.GetAddressBytes(); 
if (++bytes[3] == 0) 
    if (++bytes[2] == 0) 
    if (++bytes[1] == 0) 
     ++bytes[0]; 

IPAddress nextIPAddress = new IPAddress(bytes); 

出典:ip address increment problem

関連する問題