标题:[求助]如何实现C#循环ping多个IP,急!!
只看楼主
aya88888888
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-2-14
 问题点数:0 回复次数:7 
[求助]如何实现C#循环ping多个IP,急!!

如何实现C#循环ping多个IP

就是我在用c#编写一个监控软件,就是比如我添加ip地址(地址肯定不止一个),然后ping这些地址,(通过button开始监控)如果不通了,那么就会发出报警声音!然后颜色字体改变!现在就是最核心的部分ping而且是不间断同时循环ping多个地址,这个代码要怎么做?同时PING多个IP的包是默认大小的包,如果我想加个能够控制包的大小和PING的时间长短,单PING一个IP的功能,代码该怎么写?那位仁兄赶紧帮帮我!!!!!!跪谢!!!!!

搜索更多相关主题的帖子: ping 
2007-02-14 11:33
redcar
Rank: 2
等 级:论坛游民
帖 子:210
专家分:60
注 册:2006-9-13
得分:0 
哈哈,兄弟,我做过类似的咚咚,很遗憾,只能1次只能ping1个ip,好像不能同时ping多个,比如我要ping  10个电脑,郁闷,要等将近40-45秒
2007-02-14 11:51
aya88888888
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-2-14
得分:0 
那能不能1次只PING1个IP,但是轮巡着PING所有的IP,楼上的帮忙把你写过代码能不能借小弟学习下?
2007-02-14 12:00
lunly
Rank: 1
等 级:新手上路
帖 子:295
专家分:0
注 册:2007-1-17
得分:0 
帮顶一下

2007-02-14 12:06
aya88888888
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-2-14
得分:0 
哪位能把代码写出来给小弟看下,跪谢!!!!!
2007-02-14 12:11
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
得分:0 
我也想知道,顶顶!!

海鸽 is My Lover!!
2007-02-14 13:52
redcar
Rank: 2
等 级:论坛游民
帖 子:210
专家分:60
注 册:2006-9-13
得分:0 
以下是引用哈雷彗星在2007-2-14 12:05:14的发言:
多個一起需要用線程ping
星星写1个来看看哦
2007-02-26 11:13
zzqqrr
Rank: 6Rank: 6
来 自:299294817
等 级:侠之大者
威 望:1
帖 子:155
专家分:408
注 册:2011-6-11
得分:0 
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindwosLink
{
    public partial class Form1 : Form
    {
        
        public static int updatetime;
        public static bool fay = true;
        public Thread thread;
        public Form1()
        {
            InitializeComponent();
            Button.CheckForIllegalCrossThreadCalls = false;
            toolStripStatusLabel1.Text = "登陆时间:"+DateTime.Now.ToString();
            toolStripStatusLabel2.Text = "技术支持QQ:492581728";
        }

        private void btnstart_Click(object sender, EventArgs e)
        {
             thread = new Thread(Deal);
            thread.Start();
            
        }
        public void Deal()
        {
            btnstart.Enabled = false;
            btnstop.Enabled = true;
            updatetime = DateTime.Now.Minute;
           
            while (fay)
            {
                Thread.Sleep(Convert.ToInt32(tbtime.Text.Trim()) * 60);
                int newtime = DateTime.Now.Minute;
                int result = newtime - updatetime;
                if (result > Convert.ToInt32(tbtime.Text.Trim()))
                {
                    CmdPing(tbip.Text.Trim());
                }
                if (result > 0)
                {
                    CmdPing(tbip.Text.Trim());
                    updatetime = newtime;
                }
            }
        }
        //string ip = "211.102.90.6";

        //string strRst = CmdPing(ip);

        //Console.WriteLine(strRst);

        //Console.ReadLine();
        private void CmdPing(string strIp)
        {

            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            string pingrst;
            p.Start();
            p.StandardInput.WriteLine("ping -n 1 " + strIp);
            p.StandardInput.WriteLine("exit");
            string strRst = p.StandardOutput.ReadToEnd();
            int abc = strRst.IndexOf("(0% 丢失)");
            if (strRst.IndexOf("(0% 丢失)") != -1)
                pingrst = "连接";
            else if (strRst.IndexOf("Destination host unreachable.") != -1)
                pingrst = "无法到达目的主机";
            else if (strRst.IndexOf("请求超时。") != -1)
            {
                pingrst = "超时";
                btnstart.Enabled = true;
                btnstop.Enabled = false;
              
                labmessage.Text = "请求超时,请尽快处理!!!";
                labmessage.ForeColor = Color.Red;
                fay = false;
                bool result = PostMsgHelper.sendSms(tbphone.Text.Trim(), "您的服务器IP是:" + tbip.Text + "" + pingrst + "");
                thread.Abort();
               
                //MessageBox.Show("当前监控的服务器出现异常!!!");
            }
            else if (strRst.IndexOf("Unknown host") != -1)

                pingrst = "无法解析主机";

            else

                pingrst = strRst;

            p.Close();

            //return pingrst;

        }

59izk,com神奇百货,神奇百货网官网,shenqi,一个神奇的导购站!!
2013-09-30 09:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-120929-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020577 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved