biz_ping.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. namespace HXX.Scanner.Biz
  9. {
  10. /// <summary>
  11. /// 获取网络状态
  12. /// </summary>
  13. public class biz_ping
  14. {
  15. /// <summary>
  16. /// 超时设置
  17. /// </summary>
  18. private const int TIME_OUT = 100;
  19. /// <summary>
  20. /// 包大小
  21. /// </summary>
  22. private const int PACKET_SIZE = 512;
  23. /// <summary>
  24. /// 重试次数
  25. /// </summary>
  26. private const int TRY_TIMES = 2;
  27. /// <summary>
  28. /// 对运行后结果的过滤规则的正则
  29. /// </summary>
  30. private static Regex _reg = new Regex(@"时间=(.*?)ms", RegexOptions.Multiline | RegexOptions.IgnoreCase);
  31. /// <summary>
  32. /// 调用系统ping命令
  33. /// </summary>
  34. /// <param name="strCommandline"></param>
  35. /// <param name="packetSize"></param>
  36. /// <returns></returns>
  37. private static float LaunchPing(string strCommandline, int packetSize)
  38. {
  39. Process proc = new Process();
  40. proc.StartInfo.Arguments = strCommandline;
  41. proc.StartInfo.UseShellExecute = false;
  42. proc.StartInfo.CreateNoWindow = true;
  43. proc.StartInfo.FileName = "ping.exe";
  44. proc.StartInfo.RedirectStandardInput = true;
  45. proc.StartInfo.RedirectStandardOutput = true;
  46. proc.StartInfo.RedirectStandardError = true;
  47. proc.Start();
  48. string strBuffer = proc.StandardOutput.ReadToEnd();
  49. proc.Close();
  50. //Console.WriteLine( strCommandline );
  51. //Console.WriteLine( strBuffer );
  52. return ParseResult(strBuffer, packetSize);
  53. }
  54. /// <summary>
  55. /// 根据ping的结果计时,估算网速
  56. /// </summary>
  57. /// <param name="strBuffer"></param>
  58. /// <param name="packetSize"></param>
  59. /// <returns></returns>
  60. private static float ParseResult(string strBuffer, int packetSize)
  61. {
  62. if (strBuffer.Length < 1) return 0.0F;
  63. MatchCollection mc = _reg.Matches(strBuffer);
  64. if (mc == null || mc.Count < 1 || mc[0].Groups == null) return 0.0F;
  65. int avg;
  66. if (!int.TryParse(mc[0].Groups[1].Value, out avg)) return 0.0F;
  67. if (avg <= 0) return 1024.0F;
  68. var result= (float)packetSize / avg * 1000 / 1024;
  69. return (float)result;
  70. }
  71. /// <param name="strHost">主机名或ip</param>
  72. /// <returns>kbps/s</returns>
  73. public static float Test(string strHost)
  74. {
  75. return LaunchPing(string.Format("{0} -n {1} -l {2} -w {3}", strHost, TRY_TIMES, PACKET_SIZE, TIME_OUT), PACKET_SIZE);
  76. }
  77. /// <param name="strHost">主机名或ip</param>
  78. /// <param name="PacketSize">发送测试包大小</param>
  79. /// <param name="TimeOut">超时</param>
  80. /// <param name="TryTimes">测试次数</param>
  81. /// <returns>kbps/s</returns>
  82. public static float Test(string strHost, int PacketSize, int TimeOut, int TryTimes)
  83. {
  84. return LaunchPing(string.Format("{0} -n {1} -l {2} -w {3}", strHost, TryTimes, PacketSize, TimeOut), PacketSize);
  85. }
  86. }
  87. }