沐风心扬C#编程速查系列之Web编程专题(2)----最完美的C#网络测速(Ping)

转自:http://blog.sina.com.cn/s/blog_8a1792990100u1l2.html

要测试网速(非网页速度),发现能找到的资料几乎都是控制台的Ping和发echo包,别无他法.
要测试一台主机到客户机之间的速度无疑需要数据之间的交流.Ping(ICMP包)和echo包,主机是会返回数据的,所以可以用来测试速度.C#中用Socket实现发送icmp包的实现很多,但都不尽容人意,实现的方法实在是让人心忧忧.
ping是icmp协议的一个最有名的应用.在ms 的windows家族中提供了一个专们的ping.exe的工具,供大家差遣.
ping.exe是一个控制台程序,它能够返回执行ping动作的数据结果.
拿来主义.承ms的强大和财大气粗.我将在C#中调用ms的ping.exe程序,并分析返回的结果,得到网络的速度.代码没什么好解释的,自我陶醉一番,只要不是白痴几乎能看懂的.:(


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();
}
}
}



要测试网速(非网页速度),发现能找到的资料几乎都是控制台的Ping和发echo包,别无他法.要测试一台主机到客户机之间的速度无疑需要数据之间的交流.Ping(ICMP包)和echo包,主机是会返回数据的,所以可以用来测试速度.C#中用Socket实现发送icmp包的实现很多,但都不尽容人意,实现的方法实在是让人心忧忧.ping是icmp协议的一个最有名的应用.在ms 的windows家族中提供了一个专们的ping.exe的工具,供大家差遣.ping.exe是一个控制台程序,它能够返回执行ping动作的数据结果.拿来主义.承ms的强大和财大气粗.我将在C#中调用ms的ping.exe程序,并分析返回的结果,得到网络的速度.代码没什么好解释的,自我陶醉一番,只要不是白痴几乎能看懂的.:(

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();        }    } }

你可能感兴趣的:(ping)