ReadAsync正在清空流

时间:2018-03-24 20:57:42

标签: c# sockets cryptostream

所以,我让这段代码适用于客户端/服务器程序之间的通信。通信由一个字节缓冲区完成,该缓冲区有两个元素,一个指定跟随多少字节的int,以及" payload"。如果我用我的 调试器单步执行并检查。当我的代码到达

行时

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 );
}

1 个答案:

答案 0 :(得分:0)

Try following code :

        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; }
        }