C# ping 测试网速


using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace alonesail.PingEx
{
    public static class Ping
    {
        #region codes
        private const int TIME_OUT = 100;
        private const int PACKET_SIZE = 512;
        private const int TRY_TIMES = 2;

        private static Regex _reg = new Regex( @"时间=(.*?)ms", RegexOptions.Multiline | RegexOptions.IgnoreCase );
        private static float LaunchPing( string strCommandline, int packetSize )
        {
            Process proc = new Process();
            proc.StartInfo.Arguments = strCommandline;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.FileName = "ping.exe";
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;

            proc.Start();
            string strBuffer = proc.StandardOutput.ReadToEnd();
            proc.Close();

            //Console.WriteLine( strCommandline );
            //Console.WriteLine( strBuffer );

            return ParseResult( strBuffer, packetSize );
        }

        private static float ParseResult( string strBuffer, int packetSize )
        {
            if ( strBuffer.Length < 1 ) return 0.0F;

            MatchCollection mc = _reg.Matches( strBuffer );
            if ( mc == null || mc.Count < 1 || mc[0].Groups == null ) return 0.0F;
            int avg;
            if ( !int.TryParse( mc[0].Groups[1].Value, out avg ) ) return 0.0F;
            if ( avg <= 0 ) return 1024.0F;

            return (float)packetSize / avg * 1000 / 1024;
        }
        #endregion codes

        /// <summary>
        ///
        /// </summary>
        /// <param name="strHost">主机名或ip</param>
        /// <returns>kbps/s</returns>
        public static float Test( string strHost )
        {
            return LaunchPing( string.Format( "{0} -n {1} -l {2} -w {3}", strHost, TRY_TIMES, PACKET_SIZE, TIME_OUT ), PACKET_SIZE );
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="strHost">主机名或ip</param>
        /// <param name="PacketSize">发送测试包大小</param>
        /// <param name="TimeOut">超时</param>
        /// <param name="TryTimes">测试次数</param>
        /// <returns>kbps/s</returns>
        public static float Test( string strHost, int PacketSize, int TimeOut, int TryTimes )
        {
            return LaunchPing( string.Format( "{0} -n {1} -l {2} -w {3}", strHost, TryTimes, PacketSize, TimeOut ), PacketSize );
        }
    }
}//end classs

//调用程序
using System;
using System.Collections.Generic;
using System.Text;

namespace Ivan.PingEx
{
    class Program
    {
        static void Main( string[] args )
        {
            Console.WriteLine( Ping.Test( "
http://hi.baidu.com/gufanlf" ) );
            Console.Read();
        }
    } 

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值