乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      最完美的C#網(wǎng)絡(luò)測(cè)速(Ping)

       悟靜 2012-12-25

      要測(cè)試網(wǎng)速(非網(wǎng)頁速度),發(fā)現(xiàn)能找到的資料幾乎都是控制臺(tái)的Ping和發(fā)echo包,別無他法.
      要測(cè)試一臺(tái)主機(jī)到客戶機(jī)之間的速度無疑需要數(shù)據(jù)之間的交流.Ping(ICMP包)和echo包,主機(jī)是會(huì)返回?cái)?shù)據(jù)的,所以可以用來測(cè)試速度.C#中用Socket實(shí)現(xiàn)發(fā)送icmp包的實(shí)現(xiàn)很多,但都不盡容人意,實(shí)現(xiàn)的方法實(shí)在是讓人心憂憂.
      ping是icmp協(xié)議的一個(gè)最有名的應(yīng)用.在ms 的windows家族中提供了一個(gè)專們的ping.exe的工具,供大家差遣.
      ping.exe是一個(gè)控制臺(tái)程序,它能夠返回執(zhí)行ping動(dòng)作的數(shù)據(jù)結(jié)果.
      拿來主義.承ms的強(qiáng)大和財(cái)大氣粗.我將在C#中調(diào)用ms的ping.exe程序,并分析返回的結(jié)果,得到網(wǎng)絡(luò)的速度.代碼沒什么好解釋的,自我陶醉一番,只要不是白癡幾乎能看懂的.:(



      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( @"時(shí)間=(.*?)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">主機(jī)名或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">主機(jī)名或ip</param>
              /// <param name="PacketSize">發(fā)送測(cè)試包大小</param>
              /// <param name="TimeOut">超時(shí)</param>
              /// <param name="TryTimes">測(cè)試次數(shù)</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

      //////////////////////////////調(diào)用程序
      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();
              }
         

      }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多