阅读上的NetworkStream = 100%的CPU使用率 [英] Reading on a NetworkStream = 100% CPU usage

查看:222
本文介绍了阅读上的NetworkStream = 100%的CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读从的NetworkStream是在一个while循环。这个问题是我看到100%的CPU使用率。有什么办法来阻止这种情况发生?

下面是我到目前为止有:

 而(客户端= NULL和放大器;!&安培; client.Connected)
            {

                的NetworkStream流= client.GetStream();
                数据= NULL;

                尝试
                {
                    //检查,如果我们仍然连接。
                    如果(client.Client.Poll(0,SelectMode.SelectRead))
                    {
                        byte []的checkConn =新的字节[1];

                        如果(client.Client.Receive(checkConn,SocketFlags.Peek)== 0)
                        {
                            抛出新IOException异常();
                        }
                    }

                    如果(stream.DataAvailable)
                    {
                        //读取第一个命令
                        WriteToConsole(等待下一个命令);
                        数据= ReadStringFromClient(客户端,流);
                        WriteToConsole(接收到的命令:+数据);
                    }
                }
 

... code继续...

ReadStringFromClient code:

 私人字符串ReadStringFromClient(TcpClient的clientATF,的NetworkStream currentStream)
    {
        INT I;
        串builtString;
        byte []的stringFromClient =新的字节[256];

        如果(clientATF.Connected&安培;&安培; currentStream.CanRead)
        {

            I = currentStream.Read(stringFromClient,0,stringFromClient.Length);
            builtString = System.Text.Encoding.ASCII.GetString(stringFromClient,0,I);

        }

        其他
        {
            返回连接错误;
        }

        返回builtString;

    }
 

解决方案

你看到100%的CPU使用率的原因,就是你总是做一些事情,这是你无尽的结果while循环,没有延迟。

基本语义是:

  1. 检查,如果客户端连接
  2. 在民意调查中的客户端
  3. 检查数据是否可用
  4. 读取数据
  5. 回到步骤1

既然你在这个循环中,你总是做的事情,不管它是什么。最简单的语义是在您的循环结束做Thread.sleep()方法。这将帮助你,而无需做了许多变化。但是,您将引入一个延迟任何的睡眠时间,而不是一个真正的正确方法(但可能适合你的情况)。

正确的方法是了解异步套接字,如果你想使用的时候1个以上的插座连接合适的低CPU的高性能服务器,或者什么的。做一个快速谷歌搜索可能是最好的学习,我不记得什么特别好的副手物品。异步IO的好处是基本上,你将只使用CPU的时候,你有事情做。当接收数据时,你的方法将被调用做任何处理,你必须做的。

I am reading from a NetworkStream that is in a while loop. The issue is I am seeing 100% CPU usage. Is there any way to stop this from happening?

Here is what I have so far:

    while (client != null && client.Connected)
            {

                NetworkStream stream = client.GetStream();
                data = null;

                try
                {
                    // Check if we are still connected.
                    if (client.Client.Poll(0, SelectMode.SelectRead))
                    {
                        byte[] checkConn = new byte[1];

                        if (client.Client.Receive(checkConn, SocketFlags.Peek) == 0)
                        {
                            throw new IOException();
                        }
                    }

                    if (stream.DataAvailable)
                    {
                        //Read the first command
                        WriteToConsole("Waiting for next command");
                        data = ReadStringFromClient(client, stream);
                        WriteToConsole("Received Command: " + data);
                    }
                }

... Code continues...

ReadStringFromClient code:

   private string ReadStringFromClient(TcpClient clientATF, NetworkStream currentStream)
    {
        int i;
        string builtString;
        byte[] stringFromClient = new byte[256];

        if (clientATF.Connected && currentStream.CanRead)
        {

            i = currentStream.Read(stringFromClient, 0, stringFromClient.Length);
            builtString = System.Text.Encoding.ASCII.GetString(stringFromClient, 0, i);

        }

        else
        {
            return "Connection Error";
        }

        return builtString;

    }

解决方案

The reason you are seeing 100% CPU usage, is that you're always doing something, which is a result of your endless while loop with no delays.

The basic semantics are:

  1. Check if client is connected
  2. Poll the client
  3. Check if data is available
  4. Read the data
  5. Go back to step 1

Since you're in this loop, you're always doing something, whatever it is. The easiest semantic is to do a Thread.sleep() at the end of your loop. This will help you without having to make many changes. However, you will introduce a delay of whatever that sleep time is, and isn't really a proper way (but may suit your situation).

The proper method is to learn about asynchronous sockets if you want a high performance server, or something that uses suitable low CPU when 1 or more sockets are connected. Doing a quick Google search is probably best to learn, I don't recall any particularly good articles off hand. The benefit of Asynchronous IO is basically that you will only use CPU time when you have something to do. When data is received, your method will be called to do whatever processing you have to do.

这篇关于阅读上的NetworkStream = 100%的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆