当前位置:编程学习 > C#/ASP.NET >>

C#实现Ping功能|根据网址查询IP

 

大家应该都知道Ping是做什么的吧,如果不知道的话,你单击开始---运行---输入  ping  www.baidu.com  -t

如下图

 

\

 

当然我们也可以直接输入百度的IP进行PIng

 

\

www.zzzyk.com

 

这有什么用呢?下面咱们一起来总结一下吧

1.可以检查一下这个网站是否可以访问。

2.检查一下访问些网站的响应速度

这是我们手动通过电脑来完成的,那怎么样使用程序来完成呢?

下面我以WebServces为例子来实现

我们先来新建一个WebServces

当然你也可以新建一个网站,或者是Cs的程序都是没有任何问题的,同样都可以使用下面的代码。

现在我们先来新建 一个类PingServices,主要是完成Ping 的操作,我们使用访问Exe程序的方式,我们如果打开Windows的System32目录就能找到有这样一个程序"ping.exe"

下面就是怎么访问的问题了,我们可以使用C#自带的

   Process proc_Ping = new Process();

   Process 类可以直接访问Exe程序,定义几个变量

        //超时时间

        private const int TIME_OUT = 100;

        //包大小

        private const int PACKET_SIZE = 32;

        //Ping的次数

        private const int TRY_TIMES = 1;

 

        //取时间的正则

        private static Regex _reg = new Regex(@"时间=(.*?)ms", RegexOptions.Multiline | RegexOptions.IgnoreCase);

有注释大家一看就知道了,看下面的设置方法

   ///<summary>

        /// 设属性

        ///</summary>

        ///<param name="strCommandline">传入的命令行</param>

        private void SetProcess(string strCommandline)

        {

            //命令行

            proc_Ping.StartInfo.Arguments = strCommandline;

            //是否使用操作系统外壳来执行

            proc_Ping.StartInfo.UseShellExecute = false;

            //是否在新窗口中启动

            proc_Ping.StartInfo.CreateNoWindow = true;

            //exe名称默认的在System32下

            proc_Ping.StartInfo.FileName = "ping.exe";

            proc_Ping.StartInfo.RedirectStandardInput = true;

            proc_Ping.StartInfo.RedirectStandardOutput = true;

            proc_Ping.StartInfo.RedirectStandardError = true;

        }

有了上面的这个方法我们接下来就应该是执行了,

        ///<summary>

        /// 得到Ping的结果包括统计信息

        ///</summary>

        ///<param name="strCommandline">传入的命令行</param>

        ///<param name="packetSize">包的大小</param>

        ///<returns>KB</returns>

        private string LaunchPingStr(string strCommandline, int packetSize)

        {

            SetProcess(strCommandline);

            proc_Ping.Start();

            string strBuffer = proc_Ping.StandardOutput.ReadToEnd();

            proc_Ping.Close();

            return strBuffer;

        }

通过这个方法我们就能得到Ping的结果了,效果和上面的直接写ping www.baidu.com -t的效果是一样的。

我们在测试之前先来看下这行命令www.baidu.com -n(次数)1 -1(发送的包大小)  100 -w(超时时间)100 

如果把这行命令直接发送到ping.exe会得到什么结果呢?

在执行前我们还得来调用下这个方法

现在打开我们刚才新建的WebServces,直接放入这样一个方法

///<summary>

        /// 得到Ping结果

        ///</summary>

        ///<param name="strHost">主机名或ip</param>

        ///<param name="PacketSize">发送测试包大小</param>

        ///<param name="TimeOut">超时</param>

        ///<param name="TryTimes">测试次数</param>

        ///<returns>字符串</returns>

        [WebMethod]

        public string GetPingStr(string strHost, int PacketSize, int TimeOut, int TryTimes)

        {

            PingServices objPingServices=new PingServices ();

            string result = objPingServices.GetPingStr(strHost, PacketSize, TimeOut, TryTimes);

            return result;

        }

我们执行一下看看会得到什么结果

参数分别填入strHost=www.baidu.com, PacketSize=32, TimeOut=100,  TryTimes=5

使用的方法如下

    ///<summary>

        /// 得到Ping结果字符串

        ///</summary>

        ///<param name="strHost">主机名或ip</param>

        ///<param name="PacketSize">发送测试包大小</param>

        ///<param name="TimeOut">超时</param>

        ///<param name="TryTimes">测试次数</param>

        ///<returns>kbps/s</returns>

        public string GetPingStr(string strHost, int PacketSize, int TimeOut, int TryTimes)

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,