免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
最完美的C#網(wǎng)絡測速(Ping)

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



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">發(fā)送測試包大小</param>
        /// <param name="TimeOut">超時</param>
        /// <param name="TryTimes">測試次數(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

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

}

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
用System.Net.Mail發(fā)送郵件
C# 調用命令行執(zhí)行Cmd命令的操作
C# 分割視頻
C#中執(zhí)行批處理文件(*.bat)的方法代碼
java獲取mac地址
C# 鍵盤監(jiān)視+剪切板編程
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服