Program.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace TCPIPTest
  10. {
  11. internal class Program
  12. {
  13. [DllImport("winmm.dll")]
  14. static extern uint timeBeginPeriod(uint period);
  15. static void Main(string[] args)
  16. {
  17. bool ISfrist = true;
  18. timeBeginPeriod(1);
  19. TcpClientClass tcpClient = new TcpClientClass();
  20. Console.WriteLine("TCPIP测试程序,请输入服务端IP地址:");
  21. string serverIp = Console.ReadLine();
  22. //const string serverIp = "192.168.1.10";
  23. Console.WriteLine("TCPIP测试程序,请输入服务端端口:");
  24. int port = Convert.ToInt32(Console.ReadLine());
  25. //const int port = 502;
  26. //const string serverIp = "127.0.0.1";
  27. //const int port = 8080;
  28. if(tcpClient.CreatTcpClient(serverIp, port))
  29. {
  30. Console.WriteLine("已连接到服务器");
  31. }
  32. else
  33. {
  34. Console.WriteLine("无法连接到服务器");
  35. Console.ReadKey();
  36. return;
  37. }
  38. DateTime starttime = new DateTime();
  39. DateTime endtime = new DateTime();
  40. TimeSpan time = new TimeSpan();
  41. TimeSpan MaxTime = TimeSpan.MinValue;
  42. int Timeslow4 = 0;
  43. int TimesIn4_6 = 0;
  44. int TimesIn6_8 = 0;
  45. int TimesUp8 = 0;
  46. int count = -1;
  47. while (true)
  48. {
  49. if (count < 0)
  50. {
  51. if(!ISfrist)
  52. {
  53. // 发送消息
  54. Console.WriteLine($"最大耗时: {MaxTime.TotalMilliseconds}ms");
  55. Console.WriteLine($"耗时小于4ms的次数:{Timeslow4}");
  56. Console.WriteLine($"耗时大于4ms且小于6ms的次数:{TimesIn4_6}");
  57. Console.WriteLine($"耗时大于6ms且小于8ms的次数:{TimesIn6_8}");
  58. Console.WriteLine($"耗时大于8ms的次数:{TimesUp8}");
  59. Timeslow4 = TimesIn4_6 = TimesIn6_8 = TimesUp8 = 0;
  60. }
  61. else
  62. {
  63. ISfrist = false;
  64. }
  65. Console.Write("输入消息 (输入exit退出): ");
  66. string message = Console.ReadLine();
  67. if (message.ToLower() == "exit") break;
  68. count = Convert.ToInt32(message) -1;
  69. }
  70. ushort sendmessage = new ushort();
  71. sendmessage |= (ushort)(1 << count % 8);
  72. starttime = DateTime.Now;
  73. tcpClient.SendMessage(sendmessage);
  74. count--;
  75. Thread.Sleep(2);
  76. byte[] data = tcpClient.ReadMessage();
  77. endtime = DateTime.Now;
  78. Console.WriteLine($"{DateTime.Now.ToString("O")}服务器响应:{data[0]},{data[1]}");
  79. time = endtime - starttime;
  80. if (time.TotalMilliseconds < 4)
  81. {
  82. Timeslow4++;
  83. }
  84. else if(time.TotalMilliseconds>=4 && time.TotalMilliseconds < 6)
  85. {
  86. TimesIn4_6++;
  87. }
  88. else if (time.TotalMilliseconds >= 6 && time.TotalMilliseconds < 8)
  89. {
  90. TimesIn6_8++;
  91. }
  92. else if (time.TotalMilliseconds >= 8 )
  93. {
  94. TimesUp8++;
  95. }
  96. Console.WriteLine($"耗时: {time.TotalMilliseconds}ms");
  97. MaxTime = MaxTime>time?MaxTime:time;
  98. }
  99. }
  100. }
  101. }