ReadAsync正在排空流



所以,我让此代码正常用于客户端/服务器程序之间的通信。通信是用一个具有两个元素的字节缓冲区完成的,一个元素指定了多少个字节,并且"有效负载"。如果我用我的调试器逐步检查并检查。当我的代码到达行

await cStream.ReadAsync(bytes, 0, 4);

如果我检查可用的字节数量,它说32。一旦我经过此行,它说可用的字节为0。如果我直接从_stream(我的tcpclient的NetworkStream(读取(,则代码可以正常工作。

完整代码是

private async void RecvAsync()
{
    byte[]          key     = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
    byte[]          iv      = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
    RijndaelManaged crypto  = new RijndaelManaged();
    CryptoStream    cStream = new CryptoStream(_stream, crypto.CreateDecryptor(key, iv), CryptoStreamMode.Read);
    int bytesRead = 0;
    do
    {
        try
        {
            byte[] bytes = new byte[4];
            await cStream.ReadAsync( bytes, 0, 4 );
            //await _stream.ReadAsync(bytes, 0, 4, _cancelToken.Token);
            int size = BitConverter.ToInt32(bytes, 0);

            cStream = new CryptoStream(_stream, crypto.CreateDecryptor(key, iv), CryptoStreamMode.Read);
            bytes = new byte[size];
            bytesRead = cStream.Read( bytes, 0, size );
            //await _stream.ReadAsync(bytes, 0, bytes.Length, _cancelToken.Token); //_stream.Read( bytes, 0, size );
            int id   = BitConverter.ToInt32( bytes, 0 );
            int type = BitConverter.ToInt32( bytes, 4 );
            UTF8Encoding utf  = new UTF8Encoding();
            string       body = utf.GetString( bytes, 8, size - 8 );
            Packet packet = new Packet( id, (ServerDataType)type, body );
            ProcessPacket( packet );
        }
        catch ( Exception ) { break; }
    } while ( bytesRead > 0 );
}

尝试以下代码:

        private async void RecvAsync()
        {
            byte[]          key     = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
            byte[]          iv      = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
            RijndaelManaged crypto  = new RijndaelManaged();
            CryptoStream    cStream = new CryptoStream(_stream, crypto.CreateDecryptor(key, iv), CryptoStreamMode.Read);
            int bytesRead = 0;
            byte[] bytes = new byte[8];
            List<byte> buffer = new List<byte>();
            cStream.Read( bytes, 0, 4 );
            //await _stream.ReadAsync(bytes, 0, 4, _cancelToken.Token);
            int size = BitConverter.ToInt32(bytes, 0);
            do
            {
                bytesRead = cStream.Read( bytes, 0, 8 );
                buffer.AddRange(bytes.Take(bytesRead));
            } while ( buffer.Count() < size );
            try
            {
                if(buffer.Count() >= 8)
                {
                    int id   = BitConverter.ToInt32( buffer.ToArray(), 0 );
                    int type = BitConverter.ToInt32( buffer.ToArray(), 4 );
                    UTF8Encoding utf  = new UTF8Encoding();
                    string  body = utf.GetString( buffer.ToArray(), 8, size - 8 );
                    Packet packet = new Packet( id, (ServerDataType)type, body );
                    ProcessPacket( packet );
                }
            }
            catch ( Exception ) { break; }
        }

最新更新