.net网络速度测试-转

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
 
namespace ConsoleApplication1
{
    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);
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Ping.Test("baidu.com"));
            Console.Read();
        }
    }
 
}

  

posted on   chuncn  阅读(748)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
历史上的今天:
2009-11-16 简单做(ZTD)的十个好习惯总结--转

导航

< 2011年11月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示