如何与QUOT;串联"或QUOT;结合"或QUOT;加盟"一系列“binarily”序列化的字节数组的? [英] How to "concatenate" or "combine" or "join" a series of 'binarily' serialized byte arrays?

查看:179
本文介绍了如何与QUOT;串联"或QUOT;结合"或QUOT;加盟"一系列“binarily”序列化的字节数组的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
是从局限于单一发送命令?

请注意:我看这个问题很复杂(但愿不是为你们这些家伙,这就是为什么林询问这里大声笑),我尽我所能解释简单明了越好。

Note: I see this question very complicated (hopefully not for you guys, that's why Im asking here lol) and I tried my best to explain it as simple and clear as possible.

在我的申请,我不断收到一个固定大小的缓冲区的字节数组。

In my application, I'm continually receiving byte arrays in a fix sized buffer.

这一连串我收到已系列化binarily字节数组。

These series of byte arrays that I'm receiving has been serialized 'binarily'.

不过,有时收到的字节数组会比固定大小大缓冲所以我需要将当前接收的字节数组存储到一个容器和循环再接收剩余的字节数组中来了。

However, sometimes the byte array received will be bigger than the fix sized buffer so I would need to store the current received byte array into a container and loop again to receive the remaining byte arrays coming in.

我现在的问题是如何拼接或结合或连接字节数组的我接收(并存储在一个容器内,可能是一个字节数组的队列),以形成一个单一的字节数组,然后反序列他们所有的批?

My question now is how to "concatenate" or "combine" or "join" all the "batches" of byte arrays I received ( and is stored in a container, possibly a queue of byte arrays) to form a single byte array and then de-serialize them?

int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
    {
        // There might be more data, so store the data received so far.
        // If the buffer was not filled, I have to get the number of bytes received as Thorsten Dittmar was saying, before queuing it
        dataReceivedQueue.Enqueue(state.buffer);

        // Get the rest of the data.
        client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
        new AsyncCallback(ReceiveCallback_onQuery), state);
    }
else
{
    // All the data has arrived; put it in response.
    response_onQueryHistory = ByteArrayToObject(functionThatCombinesBytes(dataReceivedQueue));

    // Signal that all bytes have been received.
    receiveDoneQuery.Set();
}



state.buffer就是接收数据缓冲区。缓冲区大小的字节数组4096状态类型StateObject的。

state.buffer is buffer where data are received. buffer is a byte array of size 4096. state is of type StateObject.

ByteArrayToObject(字节[])采用反序列化接收到的数据并将其转换回它的对象形式

ByteArrayToObject(byte []) takes care of deserializing the data received and converting it back to its object form

functionThatCombinesBytes(队列),此功能将收到的字节队列,并结合的所有字节到一个字节数组

functionThatCombinesBytes(Queue) this function will receive a Queue of bytes and will "combine" all the bytes into one byte array

推荐答案

仅仅因为你正在调用 BeginReceive 与特定大小的缓冲区,并不意味着它必然完全填充缓冲区,所以它很可能是你的一些排队缓冲区实际上只能部分填充接收到的数据,其余部分是零,这几乎肯定会损坏您的组合流,如果你简单地串联在一起,因为你还没有真正存储读入缓冲区的字节数。您也似乎重用每次同一个缓冲区,所以你只是用新数据覆盖已读数据。

Just because you are calling BeginReceive with a buffer of a particular size, doesn't mean that it will necessarily entirely fill the buffer, so it's very likely that some of your queued buffers will actually only be partially filled with received data, and the remainder being zeros, this will almost certainly corrupt your combined stream if you simply concatenate them together since you're not also storing the number of bytes actually read into the buffer. You also appear to be reusing the same buffer each time, so you'll just be overwriting already-read data with new data.

因此,我要建议免去您的 dataReceivedQueue 的MemoryStream ,并使用类似:

I would therefore suggest replacing your dataReceivedQueue with a MemoryStream, and using something like:

if (bytesRead > 0)
    {
        // There might be more data, so store the data received so far.
        memoryStream.Write(state.buffer, 0, bytesRead);

        // Get the rest of the data.
        client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
        new AsyncCallback(ReceiveCallback_onQuery), state);
    }
else
{
    // All the data has arrived; put it in response.
    response_onQueryHistory = ByteArrayToObject(memoryStream.ToArray());

    // Signal that all bytes have been received.
    receiveDoneQuery.Set();
}

这篇关于如何与QUOT;串联"或QUOT;结合"或QUOT;加盟"一系列“binarily”序列化的字节数组的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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