0

所以,我让这段代码在客户端/服务器程序之间的通信上工作得很好。通信是通过一个字节缓冲区完成的,该缓冲区有两个元素,一个指定后面有多少字节的 int 和“有效负载”。如果我使用调试器单步执行并检查。当我的代码到达该行时

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

1 回答 1

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; }
        }
于 2018-03-25T18:44:01.927 回答