TcpClient BottleNeck [英] TcpClient BottleNeck

查看:235
本文介绍了TcpClient BottleNeck的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个独立的类,它保持一个TCP连接到服务器

I'm trying to create a self-contained class which maintains a Tcp connection to a server.

我用下面的类变量:

TcpClient tcpClient;
NetworkStream networkStream;
BinaryReader mReader;
BinaryWriter mWriter;

和使用下面的代码初始化它们:

And initializing them using the following code:

tcpClient = new TcpClient(host, 443);
networkStream = tcpClient.GetStream();
mReader = new BinaryReader(networkStream);
mWriter = new BinaryWriter(networkStream);

receiveMessage = new Thread(new ThreadStart(ReceiveMessages));
receiveMessage.Start();



我使用的阅读阻塞调用。来自服务器的每个数据包的前缀4个字节(一个int),它定义了精确的数据包大小。我使用的是我写的ByteBuffer名为类具有列表(字节)来存储字节,一旦他们进来,这个类有哪些拉动整数和其他类型的关闭名单的字节使用的readInt()ReadString顶部的功能()等,根据该服务器协议

I'm using blocking calls for the reading. Each packet coming from the server is prefixed with 4 bytes (an int) which define the exact packet size. I'm using a class I wrote named ByteBuffer which has a List(Byte) to store the bytes once they come in. The class has functions which pull ints and other types off of the top of the byte list using ReadInt(), ReadString() etc, according to the server protocol.

下面是接收器线程

private void ReceiveMessages()
{
    while (tcpClient.Connected)
    {
        if (tcpClient.Available >= 4)
        {
            try
            {
                ByteBuffer message = new ByteBuffer();
                message.AddBytes(mReader.ReadBytes(4));
                int mSize = message.ReadInt();
                message.AddBytes(mReader.ReadBytes(mSize - 4));
                MessageProcessor.Process(message);
            }
            catch (Exception ex)
            {
               Print(ex.Message);
            }
        }
        Thread.Sleep(1);
    }
    Print("Receiver thread terminated.");
    Reconnect();
}

有关参考,MessageProcessor的是一个静态类,它着眼于报文信息,响应适当的服务器。

For reference, the MessageProcessor is a static class which looks at the packet information and responds to the server appropriately.

我的问题是,当连接上的流量开始得到真正高,反应开始得到显著推迟。我想知道,是有什么我做错误尽可能的TCP连接是什么呢?我应该尝试编写类,而不是一个异步版本?是C#列出对象被频繁使用这个速度太慢(在ByteBuffer的)?

My problem is that when the traffic on the connection begins to get really high, the responses begin to get significantly delayed. I'm wondering, is there something I'm doing incorrectly as far as the tcp connection is concerned? Should I try writing an asynchronous version of the class instead? Is the C# List object too slow to be used this frequently (in the ByteBuffer)?

这真的是我在网络编程的第一次尝试,所以任何的建议是非常有帮助

This is really my first attempt at network programming, so any advice would be extremely helpful.

感谢。

推荐答案

我会重写你ReceiveMessages方法一样所以
删除了Thread.Sleep这是坏的。使用字节数组这是更快的。

I would rewrite your ReceiveMessages method like so Removes the Thread.Sleep which is bad. Uses byte arrays which are faster.

就像@jgauffin说异步网络代码是好多了,但它更容易陷入困境。如果你只是用网络编程开始更好地保持它的简单。

Like @jgauffin said Async network code is much better but it is easier to mess up. If you are just starting with network programming better keep it simple.

我希望这对你的作品好。

I hope this works better for you.

注意的消息是没有4字节的包头

private void ReceiveMessages()
    {

        while (tcpClient.Connected) {
            try {

                var networkstream = tcpClient.GetStream();
                var header = new byte[4];
                networkstream.Read(header, 0, 4);

                int len = 0;
                // calculate length from header
                // Do reverse for BigEndian, for little endian remove
                Array.Reverse(header);
                len = BitConverter.ToInt32(header, 0);

                var message = new byte[len];
                networkstream.Read(message, 0, message.Length);

                // Process message

            }
            catch (Exception ex)
            {
                Print(ex.Message);
                // Exit loop something went wrong
                break;
            }
        }

        Print("Receiver thread terminated.");
        Reconnect();

    }

这篇关于TcpClient BottleNeck的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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