• 测试TestTcp


    测试TestTcp请求----处理Socket,代码如下:

    #region 测试TestTcp
    
            /// <summary>
            /// 测试TestTcp
            /// </summary>
            public static async void TestTcp()
            {
                var threadStart = new Thread(new ThreadStart(StartTestTcp))
                {
                    IsBackground = true
                };
                threadStart.Start();
                Console.WriteLine($"TestTcp开始测试数据");
    
                int test = 1;
                while (true)
                {
                    TcpClient tcpClient = new TcpClient();
                    tcpClient.Connect(IPAddress.Parse("127.0.0.1"), 1122);
                    Console.WriteLine($"TcpClient 发送数据:{test}");
                    await tcpClient.GetStream().WriteAsync(Encoding.UTF8.GetBytes($"client{test}"));
    
                    var buffer = new byte[1024];
                    var socketConnReceCount = await tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length);
                    Console.WriteLine($"TcpClient 接收数据:{Encoding.UTF8.GetString(buffer, 0, socketConnReceCount)}");
    
                    test++;
                    Thread.Sleep(2000);
                    tcpClient.Dispose();
                }
            }
    
            public static async void StartTestTcp()
            {
                TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 1122);
                tcpListener.Start();
                int test = 1;
                Console.WriteLine($"TcpListener服务端 启动监听");
                while (true)
                {
                    var tcpclient = await tcpListener.AcceptTcpClientAsync();
                    var bytes = new byte[1024];
                    var readdataCount = await tcpclient.GetStream().ReadAsync(bytes, 0, bytes.Length);
                    Console.WriteLine($"TcpListener服务端接收数据:{Encoding.UTF8.GetString(bytes, 0, readdataCount)}");
    
                    bytes = Encoding.UTF8.GetBytes($"服务端已处理{test}");
                    await tcpclient.GetStream().WriteAsync(bytes, 0, bytes.Length);
                    tcpclient.Dispose();
                }
            }
    
            #endregion

    测试结果如下:

     public class Program
        {
            static void Main(string[] args)
            {
                 #region 测试TestTcp
                TestTcp();
                #endregion
                Console.Read();
            }
        }

  • 相关阅读:
    NOIP2018游记
    NOIP2018T1DAY1——Road(并查集做法??)
    UVA11021 Tribles——概率dp
    捡石头——(期望递推)
    USACO2008mar-gold牛跑步(第k短路:A-star)
    Java中的异常处理
    Java学习手册
    各种应用层注入手段整理 【转载】
    正则表达式学习
    Run-Time Check Failure #0,The value of ESP was not properly saved 错误解决
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/14150425.html
Copyright © 2020-2023  润新知