2

我有一个 tcp 服务器应用程序,它接受更多数量的 tcp 客户端并从所有连接的客户端接收连续消息。

public void KeepListening()
    {
        while (IsRunning)
        {
                try
                {
                        Thread.Sleep(20);
                        if (tcpListener.Pending())
                        {
                            //Get the socket obtained from the tcpListener
                            TcpClient _Client = tcpListener.AcceptTcpClient();
                            NetworkStream ns = _Client.GetStream();
                            Thread.Sleep(20);
                            if (Stop_Status == false)
                            {
                                if (_Client.Connected)
                                {
                                    int DataBytes = _Client.Available;
                                    byte[] data = new byte[DataBytes];
                                    if (ns.DataAvailable == true)
                                    {
                                        ns.Read(data, 0, DataBytes);
                                        string Key = ASCIIEncoding.ASCII.GetString(data);
                                        string[] KeyCodes = Key.Split(',');
                                        if (Key != "" && ((KeyCodes[0].StartsWith("i"))))
                                            ProcessClient(_Client, KeyCodes[0], Key);
                                    }
                                }
                            }
                            else
                            {
                                ns.Flush();
                            }
                        }
                }
                catch
                {

                }
        }
    }


    void ProcessClient(TcpClient _Client, string Key,string Msg)
    {
        try
        {
            if (hashTable_Users.Count > 0)
            {
                if (hashTable_Users.Contains(Key))
                {
                    hashTable_Users.Remove(Key);
                }
            }
            hashTable_Users.Add(Key, _Client);
            hashusers = hashTable_Users.Count;
            this.MessageFromClient(Key, serverPortNo.ToString(), Msg);

            //StreamWriter sw = new StreamWriter(_Client.GetStream());
            //sw.WriteLine("1");
            //sw.Flush();

            object[] objarr = new object[5];
            objarr[0] = _Client;
            objarr[1] = Key;
            Thread ReceiveFromClient = new Thread(new ParameterizedThreadStart(ReceiveFromClients));
            ReceiveFromClient.Start(objarr);
        }
        catch
        {

        }
    }
   public void ReceiveFromClients(object obj)
    {
            object[] objarr = (object[])obj;
            TcpClient _Client = (TcpClient)objarr[0];
            NetworkStream ns = _Client.GetStream();
            while (IsRunning)
            {
                if (Stop_Status == false)
                {
                    if (_Client.Connected)
                    {
                        if (ns.DataAvailable)
                        {
                            try
                            {
                                int numBytesRead = _Client.Available;
                                byte[] data = new byte[numBytesRead];
                                ns.Read(data, 0, numBytesRead);
                                string MsgReceived = ASCIIEncoding.ASCII.GetString(data);
                                string clientKey = objarr[1].ToString();
                                this.MessageFromClient(clientKey, serverPortNo.ToString(), MsgReceived);
                            }
                            catch
                            {

                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    ns.Flush();
                }
            }
    }

KeepListening()用于接受发送请求的客户端。ProcessClient()ReceiveFromClients()用于接收来自客户端的消息。

此应用程序正在使用 100% 的 CPU。

请建议我怎么做才能降低cpu利用率?

4

2 回答 2

3

通过查看您的代码,ReceiveFromClients 正在消耗大部分 CPU,因为 while (IsRunning) 并且如果只有数据可用,您正在读取。

为了解决这个问题,不要检查 IsAvailabele,调用 ReadMethod 阻塞线程,直到 ReadTimeOut 发生或已读取一些数据。根据接收到的字节数,处理您的数据。

于 2012-09-03T09:35:16.720 回答
0

请建议我怎么做才能降低cpu利用率?

好吧,这看起来是一个糟糕的开始:

while (IsRunning)
{
    try
    {
        ...
    }
    catch
    {
    }
}

如果您反复遇到异常,您将永远不会知道它 - 并且继续循环。你也有另一个空的 catch 块......你真的不应该抓住所有东西然后继续前进,甚至不记录。

同样在这里:

while (IsRunning)
{
    if (Stop_Status == false)
    {
        ...
    }
    else
    {
        ns.Flush();
    }
}

如果IsRunning是真的,Stop_Status是假的,那只会紧循环,反复调用Flush......

于 2012-09-03T09:30:56.170 回答