简体   繁体   English

C#和TCP客户端(异步)具有很高的CPU负载&如何检测断开连接的客户端?

[英]C# and TCP Client (Async) has very high CPU load & How to detect disconnected clients?

the CPU load 90+ if multiple clients connect. 如果有多个客户端连接,则CPU负载为90+。 If i start the listener and have no connection, everything is great. 如果我启动监听器并且没有连接,那么一切都很好。 If i have one or more Connections i have a really high CPU load. 如果我有一个或多个Connections,我的CPU负载会很高。

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Test.Socket
{
    public class Server
    {
        List<Thread> WorkListenerThread;

        TcpListener Listener;

        public Server()
        {
            WorkListenerThread = new List<Thread>();
        }

        public void Start()
        {
            try
            {
                Listener = new TcpListener(IPAddress.Any, 12345);
                Listener.Start();
                StartTCPClientListener();
            }
            catch (Exception) { }
        }

        private void StartTCPClientListener()
        {
            Listener.BeginAcceptTcpClient(new AsyncCallback(HandleTCPClientConnection), null);
        }

        private void HandleTCPClientConnection(IAsyncResult ar)
        {
            // Problem 1: after the first connection i have a high cpu load
            try
            {
                TcpClient client = Listener.EndAcceptTcpClient(ar);
                Thread clientThread = new Thread(Communicator.CreateConnection);
                clientThread.Start(client);
                WorkListenerThread.Add(clientThread);

                StartTCPClientListener(); // Next client
            }
            catch (Exception) { }
        }

        public void Stop()
        {
            foreach (Thread th in WorkListenerThread)
                if (th.IsAlive)
                    th.Abort();

            // Problem 2: Exception because the "HandleTCPClientConnection" get a wrong IAsyncResult
            if (Listener != null)
                Listener.Stop();
        }
    }
}

And my client class. 还有我的客户班。

using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using JsonExSerializer;

namespace Test.Socket
{
    class Communicator
    {
        private TcpClient TcpClient = null;
        private NetworkStream Stream = null;

        private Communicator(TcpClient client)
        {
            TcpClient = client;
            Stream = TcpClient.GetStream();
            StartCommunication();
        }

        public static void CreateConnection(object c)
        {
            new Communicator(c as TcpClient);
        }

        private void StartCommunication()
        {
            string message; // The message
            int bytesRead; // Message length
            byte[] buffer = new byte[512]; // buffer

            while (true)
            {
                message = String.Empty;
                do
                {
                    bytesRead = Stream.Read(buffer, 0, buffer.Length);
                    message += Encoding.Default.GetString(buffer, 0, bytesRead);
                } while (!message.EndsWith("\r\n.\r\n"));

                Send("OK", Stream);
            }
        }

        protected void Send(string message, NetworkStream clientStream)
        {
            byte[] temp = Encoding.Default.GetBytes(message);
            clientStream.Write(temp, 0, temp.Length);
            clientStream.Flush();
        }
    }
}

I dont know whats wrong -.- 我不知道怎么了-.-

And anyone know how i candetect if a client is disconnected? 还有谁知道我如何检测客户端是否断开连接? That would be nice, too. 那也很好。

But fixing the high CPU Load would be great enough. 但是解决高CPU负载就足够了。

I'm guessing the main problem is here: 我猜主要问题在这里:

while (true)
{
    message = String.Empty;
    do
    {
        bytesRead = Stream.Read(buffer, 0, buffer.Length);
        message += Encoding.Default.GetString(buffer, 0, bytesRead);
    } while (!message.EndsWith("\r\n.\r\n"));

    Send("OK", Stream);
}

You should check whether this bytesRead is non-positive; 您应该检查这个bytesRead是否为非正数; if it is, that is the end of the stream and you should terminate: no more data is ever coming, but you are in a tight loop appending empty strings forever. 如果是,那是流的末尾,你应该终止:没有更多的数据是以往任何时候都来了,但你是在一个紧凑的循环追加空字符串永远。

However! 然而! The bigger problem is the approach: thread-per-client simply doesn't scale, and is not what people mean when they mention "async IO". 更大的问题是方法:每个客户端线程根本无法扩展,这不是人们提到“异步IO”时的意思。

I would also say that having a constructor that starts a long-running operation (so doesn't return) is a horrible thing to do. 我还要说,让构造函数启动长时间运行的操作(因此不会返回)是一件可怕的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM