此语法可用于 ping IP 范围吗?
我正在尝试创建一个代码,允许我对 IP 地址范围进行 ping 操作。
我收到 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();
}
I'm trying to create a code which will allow me to ping range of IP addresses.
I'm getting 2 inputs. One is address to start range from and the other is the end of the IP range I want to ping. Then I'm splitting a string and assigning the values of each to variables that will change (a, b, c, d, aa, bb, cc, dd).
Is this logically correct?
This is my code:
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你把事情想得太复杂了!我会用这样的东西来保持简单:
这
是一个例子:
You're overcomplicating it! I'd keep it simple with something like this:
and
Here's an example:
我建议您使用类 IPAddress。
您可以使用此代码迭代 IP 地址:
来源:IP 地址增量问题
I recommend you to use a class IPAddress.
Than you can iterate through IP addresses using this code:
Source: ip address increment problem