C#TCP无法从NetworkStream获取所有字节仅29200字节

时间:2018-03-05 11:48:45

标签: c# tcp networkstream

我试图从TCP响应中读取一些大数据。但我得到29200字节或字符。但是我调试了,如果调试步骤,我会得到所有数据,但是在调用时执行:

Int32 bytes = clientStream.Read(data, 0, data.Length);

但是否则我没有得到所有的字节。这是我的功能:

private string SendMessage(string msg, int buffersize)
    {
        NetworkStream clientStream = client.GetStream();

        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] buffer = encoder.GetBytes(msg);

        clientStream.Write(buffer, 0, buffer.Length);
        clientStream.Flush();

        // Buffer to store the response bytes. 
        Byte[] data = new Byte[buffersize];

        // String to store the response ASCII representation. 
        String responseData = String.Empty;

        // Read the first batch of the TcpServer response bytes. 
        Int32 bytes = clientStream.Read(data, 0, data.Length);
        responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

        return responseData;
    }

我尝试使用像这样的MemoryStream: https://stackoverflow.com/a/33203184

但我也没有得到所有字节。

我做错了什么?

0 个答案:

没有答案