如何实现C#循环ping多个IP? 就是我在用c# form编写一个监控软件循环ping不同ip,ip地址是从数据库里都出来,然后通过后台ping这些地址,最后将ping的结果保存到数据库里,能ping通就用0标记,ping不通的话就用1做标记,现在就是最核心的部分后台ping而且是不间断循环ping多个地址,这个代码要怎么做?不用显示出来ping的值,那位仁兄赶紧帮帮!!!!!!跪谢!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ping而且是不间断循环ping多个地址不考虑多线程的话可以参考下: for (i = 1; i <= 255; i++)//这里你把数据库里的ip取到dataset里面然后循环 { //s2="10.0.18."; s3 = s2 + i.ToString(); string s5 = Main1(s3); if (s5 != "") { //该ip可以ping通 } public static string Main1(string args) { Ping pingSender = new Ping(); PingOptions options = new PingOptions(); // Use the default Ttl value which is 128, // but change the fragmentation behavior. options.DontFragment = true; // Create a buffer of 32 bytes of data to be transmitted. string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 120; PingReply reply = pingSender.Send(args, timeout, buffer, options); if (reply.Status == IPStatus.Success) { return reply.Address.ToString(); //Console.WriteLine ("Address: {0}", reply.Address.ToString ()); //Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime); //Console.WriteLine ("Time to live: {0}", reply.Options.Ttl); //Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment); //Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length); } return "";} 做一个数组,存储Ip地址,然后用循环,取出这个数组,依次ping,最外层套一个while(true)循环,即一个无限循环。 en,使用线程处理你的Ping.否则会死掉默认有个组件提供后台线程服务,在winform中。 没有想象中那么简单,做个循环就可以,我是开线程处理,可每次运行所得到的结果都不一样,有漏掉的ip,但单步调试就可以跟踪到所有的ip信息. public delegate void UpdateList(string sIP, string sHostName); public partial class Form2 : Form { Thread t1; private System.DateTime StartTime; public Form2() { InitializeComponent(); } public class ping { public UpdateList ul; public string ip; //定义一个变量,用以接收传送来的IP地址字符串 public string HostName; //定义一个变量,用以向主进展传递对应IP地址是否在线数据 public void scan() { CheckForIllegalCrossThreadCalls = false; IPAddress myIP = IPAddress.Parse(ip); try { IPHostEntry myHost = Dns.GetHostByAddress(myIP); HostName = myHost.HostName.ToString(); } catch { HostName = ""; } if (HostName == "") HostName = "主机没有响应!"; if (ul != null) ul(ip, HostName); } } private void button1_Click_1(object sender, EventArgs e) { listBox1.Items.Clear(); //清楚扫描结果显示区域 label4.Text = ""; StartTime = DateTime.Now; //获取当前时间 string mask = numericUpDown1.Value.ToString() + "." + numericUpDown2.Value.ToString() + "." + numericUpDown3.Value.ToString() + "."; int Min = (int)numericUpDown4.Value; int Max = (int)numericUpDown5.Value; if (Min > Max) { MessageBox.Show("输入的IP地址区间不合法,请检查!", "错误!"); return; } //判断输入的IP地址区间是否合法 int _ThreadNum = Max - Min + 1; Thread[] mythread = new Thread[_ThreadNum]; //创建一个多个Thread实例 progressBar1.Minimum = Min; progressBar1.Maximum = Max + 1; progressBar1.Value = Min; int i; for (i = Min; i <= Max; i++) { int k = Max - i; ping HostPing = new ping(); //创建一个ping实例 HostPing.ip = mask + i.ToString(); HostPing.ul = new UpdateList(UpdateMyList); //向这个ping实例中传递IP地址字符串 t1= mythread[k] = new Thread(new ThreadStart(HostPing.scan)); //初始化一个线程实例 mythread[k].Start(); //启动线程 } } public void UpdateMyList(string sIP, string sHostName) { lock (listBox1) { if (checkBox1.Checked == true)//回显 { listBox1.Items.Add(sIP + " \t" + sHostName); } if (checkBox1.Checked == false) //不回显 { if (sHostName != "主机没有响应!") { listBox1.Items.Add(sIP + " \t" + sHostName); } if (sHostName == "主机没有响应!") { } } if (progressBar1.Value != progressBar1.Maximum) { this.progressBar1.Value++; } if (progressBar1.Value == progressBar1.Maximum) { MessageBox.Show("成功完成检测!", "提示"); DateTime EndTime = DateTime.Now; TimeSpan ts = EndTime - StartTime; label4.Text = ts.Seconds.ToString() + "秒"; //显示扫描计算机所需要的时间 progressBar1.Value = progressBar1.Minimum; } } } private void button2_Click(object sender, EventArgs e) { try { t1.Abort(); Form2 kl = new Form2(); label4.Text = "线程停止中..."; } catch { this.Dispose(); } } }} 你做延时等待了吗?ping一个IP时似乎该做一个延时等待,不然上一个IP地址的结果还没有返回,你又ping了下一个。所以丢失了吧。 在C# 2.0中实现ping功能icscs 著于2007-10-9 19:53:51本文主要是用于熟悉.NET 2.0中一个新的命名空间System.Net.NetworkInformation的使用,这个命名空间包括了一个Ping类... 对呀,干嘛不用Ping类呢,直接检测就可以! 导入csv数据,报错“外部表不是预期的格式” 系统消息如何实现? DataGridView 编辑的值不能实时更新DataSource。 如何将2个整型数转成字符串? datagridview checkbox 列 判断是否可以选中 怎么把控件存储到图片中? 最郁闷的面试过程和题目,十几道,我只回答了一道,别的都不会,汗!! C#开发SQL人事工资管理系统 求助!!急 菜鸟请教C#问题~~~` 关于调用前台js的问题? WPF的ListBox的ItemsSource已经绑定到某个数据集时,如何在代码中取得或设置选定项的某个列的值? 怎么在winform中播放音乐
不考虑多线程的话可以参考下:
for (i = 1; i <= 255; i++)//这里你把数据库里的ip取到dataset里面然后循环
{ //s2="10.0.18.";
s3 = s2 + i.ToString(); string s5 = Main1(s3); if (s5 != "")
{
//该ip可以ping通
}
public static string Main1(string args)
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions(); // Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true; // Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120; PingReply reply = pingSender.Send(args, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
return reply.Address.ToString();
//Console.WriteLine ("Address: {0}", reply.Address.ToString ());
//Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
//Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
//Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
//Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
}
return "";
}
然后用循环,取出这个数组,依次ping
,最外层套一个while(true)循环,即一个无限循环。
默认有个组件提供后台线程服务,在winform中。
public partial class Form2 : Form
{
Thread t1;
private System.DateTime StartTime; public Form2()
{
InitializeComponent();
} public class ping
{
public UpdateList ul;
public string ip;
//定义一个变量,用以接收传送来的IP地址字符串 public string HostName; //定义一个变量,用以向主进展传递对应IP地址是否在线数据 public void scan()
{
CheckForIllegalCrossThreadCalls = false; IPAddress myIP = IPAddress.Parse(ip); try
{ IPHostEntry myHost = Dns.GetHostByAddress(myIP);
HostName = myHost.HostName.ToString(); } catch
{ HostName = ""; } if (HostName == "") HostName = "主机没有响应!"; if (ul != null)
ul(ip, HostName);
} } private void button1_Click_1(object sender, EventArgs e)
{
listBox1.Items.Clear();
//清楚扫描结果显示区域
label4.Text = "";
StartTime = DateTime.Now; //获取当前时间 string mask = numericUpDown1.Value.ToString() + "." + numericUpDown2.Value.ToString() + "." + numericUpDown3.Value.ToString() + "."; int Min = (int)numericUpDown4.Value; int Max = (int)numericUpDown5.Value; if (Min > Max)
{ MessageBox.Show("输入的IP地址区间不合法,请检查!", "错误!"); return; } //判断输入的IP地址区间是否合法 int _ThreadNum = Max - Min + 1; Thread[] mythread = new Thread[_ThreadNum]; //创建一个多个Thread实例 progressBar1.Minimum = Min; progressBar1.Maximum = Max + 1; progressBar1.Value = Min; int i; for (i = Min; i <= Max; i++)
{ int k = Max - i; ping HostPing = new ping(); //创建一个ping实例 HostPing.ip = mask + i.ToString(); HostPing.ul = new UpdateList(UpdateMyList); //向这个ping实例中传递IP地址字符串 t1= mythread[k] = new Thread(new ThreadStart(HostPing.scan)); //初始化一个线程实例
mythread[k].Start(); //启动线程
}
} public void UpdateMyList(string sIP, string sHostName)
{
lock (listBox1)
{
if (checkBox1.Checked == true)//回显
{
listBox1.Items.Add(sIP + " \t" + sHostName);
}
if (checkBox1.Checked == false) //不回显
{
if (sHostName != "主机没有响应!")
{
listBox1.Items.Add(sIP + " \t" + sHostName);
}
if (sHostName == "主机没有响应!")
{
}
}
if (progressBar1.Value != progressBar1.Maximum)
{
this.progressBar1.Value++;
}
if (progressBar1.Value == progressBar1.Maximum)
{
MessageBox.Show("成功完成检测!", "提示");
DateTime EndTime = DateTime.Now;
TimeSpan ts = EndTime - StartTime;
label4.Text = ts.Seconds.ToString() + "秒";
//显示扫描计算机所需要的时间
progressBar1.Value = progressBar1.Minimum;
}
} } private void button2_Click(object sender, EventArgs e)
{
try
{
t1.Abort();
Form2 kl = new Form2();
label4.Text = "线程停止中...";
}
catch
{
this.Dispose();
}
}
}
}
icscs 著于2007-10-9 19:53:51
本文主要是用于熟悉.NET 2.0中一个新的命名空间System.Net.NetworkInformation的使用,这个命名空间包括了一个Ping类...