zoukankan      html  css  js  c++  java
  • 关于 Tcp 收发 异步 和同步的对比

    服务器代码

    class Program
        {
            static int count = 0;
            static DateTime dt = DateTime.Now;
            static void Main(string[] args)
            {
                TcpListener server = new TcpListener(new IPEndPoint(IPAddress.Any, 11220));
                server.Start();
               // StartAcceptAsync(server);
    
                 StartAccept(server);
                Console.Read();
            }
            static void StartAcceptAsync(TcpListener s)
            {
                var v = s.AcceptTcpClientAsync();
                v.ContinueWith((e) =>
                {
                    NetworkStream ns = e.Result.GetStream();
                    StartReadAsync(ns);
                    StartAcceptAsync(s);
                    Console.WriteLine("收到一个链接");
                });
            }
            static void StartReadAsync(NetworkStream ns)
            {
                byte[] buff = new byte[1024];
                ns.ReadAsync(buff, 0, 1024).ContinueWith((e) =>
                {
                    if (count == 0)
                        dt = DateTime.Now;
                    try
                    {
                        count += e.Result;
                    }
                    catch { return;}
                    Console.WriteLine("count=" + count + ",dt=" + (DateTime.Now - dt).TotalMilliseconds);
                    StartReadAsync(ns);
                });
            }
            static void StartAccept(TcpListener s)
            {
                Task.Run(() =>
                {
                    while (true)
                    {
                        TcpClient client = s.AcceptTcpClient();
                        StartRead(client.GetStream());
                    }
                });
            }
            static void StartRead(NetworkStream ns)
            {
                Task.Run(() =>
                {
                    while (true)
                    {
                        if (count == 0)
                            dt = DateTime.Now;
                        byte[] data = new byte[1024];
                        try
                        {
                            int x = ns.Read(data, 0, 1024);
                            count += x;
                        }
                        catch { return;}
                       
                        Console.WriteLine("count=" + count + ",dt=" + (DateTime.Now - dt).TotalMilliseconds);
                    }
                });
            }
        }

    客户端 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.Net.Sockets;
    namespace ConsoleApp4
    {
        class Program
        {
            static int count = 0;
            static void Main(string[] args)
            {
                for (int i = 0; i < 10; i++)
                    InitTcp();
                Console.Read();
            }
            static void InitTcp()
            {
                TcpClient tcp = new TcpClient();
                tcp.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"),11220));
                NetworkStream ns= tcp.GetStream();
                Task.Run(() =>
                {
                    for (int i = 0; i < 10000; i++)
                    {
                        ns.WriteByte(0x01);
                        count++;
                        Console.WriteLine("Send Count=" + count);
                    }
                });
            }
        }
    }

    同步 

              cpu  :

               结果:

    异步 

               cpu:

               结果:

        异步与同步的 cpu占用差距不大可能我我模拟的客户端不够多的原因,

        异步比同步接收的更快,

    重新测试了100个客户端 就出现问题,结果 同步远远低于异步,后来发现是 task 类 只使用了20个线程在调度,后改成 Thread 发现速度跟 异步基本一致

    后修改threadpool的 最大最小线程数量  继续使用 task,和异步 发现 task比之前的异步要快 ,但是同样修改了线程数量以后的 异步 速度快了3倍左右

    同线程数量 异步绝对快于同步  在线程少于客户端连接时 异步大概是同步的100倍  线程多余 客户端连接时 异步大概是 同步的 3倍,

    在客户端比较少的时候两者无区别

  • 相关阅读:
    Qt中使用cout, cin, cerr
    linux下清理系统垃圾
    linux清理内存命令
    boost 特点
    linux boost 安装
    valgrind 的使用及错误信息分析
    ArcGIS Engine 编辑介绍
    ArcGIS Engine 编辑- IWorkspaceEdit
    ArcGIS Engine 编辑- ITask
    CreateFeature与CreateFeatureBuffer区别
  • 原文地址:https://www.cnblogs.com/xiongyang123/p/11164028.html
Copyright © 2011-2022 走看看