【问题标题】:Could this syntax be used to ping range of IPs?此语法可用于 ping IP 范围吗?
【发布时间】:2012-01-18 20:46:12
【问题描述】:

我正在尝试创建一个允许我 ping IP 地址范围的代码。

我得到 2 个输入。一个是开始范围的地址,另一个是我要 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();
}

【问题讨论】:

  • 技术上没有,因为你从不声明变量 a、b、c、d、aa、bb、cc、dd

标签: c# winforms if-statement ping


【解决方案1】:

你太复杂了!我会用这样的方式保持简单:

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();
}

【讨论】:

  • 这是一个很好的例子。当我尝试使用你的代码时,我会给出反馈。
  • 聪明,但我不会说这很简单。我担心,移位和将四个字节编码为无符号整数的概念对许多程序员来说是陌生的。
  • @Igby Largeman,我有点同意。但是这个概念很容易概括。这里唯一困难的部分是转换为 uint,反之亦然。
【解决方案2】:

我建议你使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    相关资源
    最近更新 更多