就是我在用c# form编写一个监控软件循环ping不同ip,ip地址是从数据库里都出来,然后通过后台ping这些地址,最后将ping的结果保存到数据库里,能ping通就用0标记,ping不通的话就用1做标记,现在就是最核心的部分后台ping而且是不间断循环ping多个地址,这个代码要怎么做?不用显示出来ping的值,那位仁兄赶紧帮帮!!!!!!跪谢!!!!!
解决方案 »
- 请指教:数据查询的构思
- 类似提取网页信息-提取猫扑回复的邮箱
- 怎样清空文件里的数据、??
- 页面动态生成文本框
- 困绕了无数高手多年的问题(如何判断WebBrowser下载完成)
- 菜菜紧急求救!!!在线等!
- 如何新起一个线程来操作文件??
- 调试带参数的main()方法
- WINDOWS CE系统中,如何用C#来访问远程的oracel数据库呢?
- 无法使用dataGridView,报错empted to read or write protected memory
- 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类...