该C#开发工具实现功能:
1、可以扫描制定C网段的地址扫描,使用ping方式进行探测
2、统计对ICMP有响应的和无响应的主机数量
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Net.NetworkInformation;
using System.IO;
namespace PingIP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//关闭跨线程检查,正式开放环境中不允许这么做,只能通过回调机制来实现。
TextBox.CheckForIllegalCrossThreadCalls = false;
}
#region //开始按钮操作
//定义个线程,用于执行具体的操作。
Thread thread = null;
private void BtnStart_Click(object sender, EventArgs e)
{
//清空输出窗口内容
lbOutPut.Items.Clear();
//定义“未活动或不可达IP个数:”变量
txtip6.Text = null;
//定义“活动的IP个数:”变量
txtip7.Text = null;
//点击开始按钮后,禁用该按钮,防止用户重复点击
BtnStart.Enabled = false;
//实例化线程
thread = new Thread(Scanip);
//设置为后台线程
thread.IsBackground = true;
//启动线程
thread.Start();
}
#endregion
//定义ping的方法
void Scanip()
{
//定义IP地址输入框
int ip1 = Convert.ToInt32(txt1.Text);
int ip2 = Convert.ToInt32(txt2.Text);
int ip3 = Convert.ToInt32(txt3.Text);
int ip4 = Convert.ToInt32(txt4.Text);
int ip5 = Convert.ToInt32(txt5.Text);
int ip6 = 0;
int ip7 = 0;
//实例化一个ping实例
Ping ping = new Ping();
//定义ping的选项,其中包含ttl值和是否分段
PingOptions poption = new PingOptions(128, true);
//定义接收数组
byte[] data = new byte[32];
//定义变量
int recived = 0;
//实例化一个响应时间变量
List<long> responseTimes = new List<long>();
//循环执行IP地址段的每个ip的ping操作
for (int j = ip4; j <= ip5; j++)
{
//实例化IP地址
IPAddress address = IPAddress.Parse(ip1 + "." + ip2 + "." + ip3 + "." + ip4);
//最后一个IP地址段自增
ip4++;
//每个ip执行一次ping操作
for (int i = 0; i < 1; i++)
{
//防止中断ping操作。
try
{
//实例化一个reply
PingReply reply = ping.Send(address, 1000, data, poption);
string str = null;
if (reply != null)
{
//对reply状态进行分类
switch (reply.Status)
{
case IPStatus.Success:
str = string.Format("Reply From{0}" + "Bytes={1} time={2} TTL={3}", reply.Address, reply.Buffer.Length, reply.RoundtripTime, reply.Options.Ttl);
lbOutPut.Items.Add(str);
lbOutPut.SelectedItem = lbOutPut.Items[lbOutPut.Items.Count - 1];
this.Update();
recived++;
responseTimes.Add(reply.RoundtripTime);
ip7++;
txtip7.Text = ip7.ToString();
break;
case IPStatus.TimedOut:
str = address.ToString() + ":Request time Out";
lbOutPut.Items.Add(str);
lbOutPut.SelectedItem = lbOutPut.Items[lbOutPut.Items.Count - 1];
this.Update();
ip6++;
txtip6.Text = ip6.ToString();
break;
default:
str = string.Format("Ping Failed{0}", reply.Status.ToString());
lbOutPut.Items.Add(str);
lbOutPut.SelectedItem = lbOutPut.Items[lbOutPut.Items.Count - 1];
this.Update();
ip6++;
txtip6.Text = ip6.ToString();
break;
}
}
else
{
str = string.Format("Ping Failed for an unkown reason");
lbOutPut.Items.Add(str);
lbOutPut.SelectedItem = lbOutPut.Items[lbOutPut.Items.Count - 1];
this.Update();
ip6++;
txtip6.Text = ip6.ToString();
}
}
catch
{//启用开始按钮
BtnStart.Enabled = true;
break;
}
}
}
//启用开始按钮
BtnStart.Enabled = true;
}
#region //公共事件,只允许输入数字
private void cubox_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox cubox = sender as TextBox;
if (cubox.TextLength > 2)
{
e.Handled = true;
}
//if (Convert.ToInt32(cubox.Text) > 254)
//{
// e.Handled = true;
//}
if (e.KeyChar < '0' || e.KeyChar > '9')
{
e.Handled = true;
}
if (e.KeyChar == '.')
{
this.SelectNextControl(cubox, true, true, true, true);
}
if(cubox.SelectionStart==0&&e.KeyChar=='0')
{
e.Handled=true;
}
if (e.KeyChar == 8)
{
if (cubox.TextLength == 0)
{
this.SelectNextControl(cubox, false, true, true,false);
}
e.Handled = false;
}
}
#endregion
//停止按钮的事件
private void btnStop_Click(object sender, EventArgs e)
{
thread.Abort();
}
}
}