| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Sockets;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace TCPIPTest
- {
- internal class Program
- {
- [DllImport("winmm.dll")]
- static extern uint timeBeginPeriod(uint period);
- static void Main(string[] args)
- {
- bool ISfrist = true;
- timeBeginPeriod(1);
- TcpClientClass tcpClient = new TcpClientClass();
- Console.WriteLine("TCPIP测试程序,请输入服务端IP地址:");
- string serverIp = Console.ReadLine();
- //const string serverIp = "192.168.1.10";
- Console.WriteLine("TCPIP测试程序,请输入服务端端口:");
- int port = Convert.ToInt32(Console.ReadLine());
- //const int port = 502;
- //const string serverIp = "127.0.0.1";
- //const int port = 8080;
- if(tcpClient.CreatTcpClient(serverIp, port))
- {
- Console.WriteLine("已连接到服务器");
- }
- else
- {
- Console.WriteLine("无法连接到服务器");
- Console.ReadKey();
- return;
- }
- DateTime starttime = new DateTime();
- DateTime endtime = new DateTime();
- TimeSpan time = new TimeSpan();
- TimeSpan MaxTime = TimeSpan.MinValue;
- int Timeslow4 = 0;
- int TimesIn4_6 = 0;
- int TimesIn6_8 = 0;
- int TimesUp8 = 0;
- int count = -1;
- while (true)
- {
- if (count < 0)
- {
- if(!ISfrist)
- {
- // 发送消息
- Console.WriteLine($"最大耗时: {MaxTime.TotalMilliseconds}ms");
- Console.WriteLine($"耗时小于4ms的次数:{Timeslow4}");
- Console.WriteLine($"耗时大于4ms且小于6ms的次数:{TimesIn4_6}");
- Console.WriteLine($"耗时大于6ms且小于8ms的次数:{TimesIn6_8}");
- Console.WriteLine($"耗时大于8ms的次数:{TimesUp8}");
- Timeslow4 = TimesIn4_6 = TimesIn6_8 = TimesUp8 = 0;
- }
- else
- {
- ISfrist = false;
- }
- Console.Write("输入消息 (输入exit退出): ");
- string message = Console.ReadLine();
- if (message.ToLower() == "exit") break;
- count = Convert.ToInt32(message) -1;
- }
- ushort sendmessage = new ushort();
- sendmessage |= (ushort)(1 << count % 8);
- starttime = DateTime.Now;
- tcpClient.SendMessage(sendmessage);
- count--;
- Thread.Sleep(2);
- byte[] data = tcpClient.ReadMessage();
- endtime = DateTime.Now;
- Console.WriteLine($"{DateTime.Now.ToString("O")}服务器响应:{data[0]},{data[1]}");
- time = endtime - starttime;
- if (time.TotalMilliseconds < 4)
- {
- Timeslow4++;
- }
- else if(time.TotalMilliseconds>=4 && time.TotalMilliseconds < 6)
- {
- TimesIn4_6++;
- }
- else if (time.TotalMilliseconds >= 6 && time.TotalMilliseconds < 8)
- {
- TimesIn6_8++;
- }
- else if (time.TotalMilliseconds >= 8 )
- {
- TimesUp8++;
- }
- Console.WriteLine($"耗时: {time.TotalMilliseconds}ms");
- MaxTime = MaxTime>time?MaxTime:time;
- }
- }
- }
- }
|