【问题标题】:How to asynchronously receive complex object in C#?如何在 C# 中异步接收复杂对象?
【发布时间】:2012-11-19 11:22:59
【问题描述】:

编辑: A more concise explanation of what I was trying to do here and with answers

我正在使用 c# 异步套接字从源接收数据。

我的问题是如果要接收更多数据,如何以及在哪里存储接收到的数据?

收到字符串后,我可以使用字符串生成器从 msdn 接收和存储,就像这样:

private void ReceiveCallback_onQuery(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket 
                // from the asynchronous state object.
                StateObject state = (StateObject)ar.AsyncState;
                Socket client = state.workSocket;

                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);

                if (bytesRead > 0)
                {
                    // There might be more data, so store the data received so far.
                    dataReceived += state.buffer; //Does not work (dataReceived is byte[] type)

                    // 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.
                    if (dataReceived > 1)
                    {
                        response_onQueryHistory = ByteArrayToObject(dataReceived)
                    }
                    // Signal that all bytes have been received.
                    receiveDoneQuery.Set();
                    }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

我不想将收到的数据转换为字符串,因为我收到的是一个复杂的对象。

发送的数据是序列化的,我也可以反序列化。

我的问题是如何“持续”从套接字接收数据而不使用字符串生成器来存储它。

谢谢!

【问题讨论】:

  • 我在这里看不到任何“复杂对象”,任何类型的序列化/反序列化代码,也没有对来自网络的数据类型的描述,因此很难回答您的问题。
  • 你可能想研究序列化,即aviadezra.blogspot.ie/2008/07/…...你还没有真正提出一个有效的问题
  • 什么是复杂对象?很有可能无论您发送什么,它首先被序列化为这样发送。您需要在收到它后对其进行反序列化。既然您将它放在缓冲区中......应该很容易将它提供给您选择的反序列化器吗?
  • 感谢您的评论。我只想知道如何将从 state.buffer 收到的数据存储到变量中。在我的问题中,state.buffer 中的数据存储在字符串生成器中吗?如果我不想使用字符串生成器存储它怎么办?
  • 感谢回复,是的,发送的数据已序列化。但是,我不知道我是否已收到所有数据,因此我可能不得不再次调用接收数据的函数。现在,我将把以前收到的数据存储在哪里?

标签: c# sockets asynchronous


【解决方案1】:

这取决于复杂事物在被分解字节中向下推之前如何序列化,您将接收这些字节并使用用于序列化事物的相同算法/技术将其反序列化回其原始状态.

对于更具体的答案,我会要求您自己更具体。

【讨论】:

    【解决方案2】:
    My problem is how and where to store received data if there are more to be received?
    

    可以使用 Buffer.BlockCopy 并将其排队,例如,

               int rbytes = client.EndReceive(ar);
    
                if (rbytes > state.buffer)
                {
                    byte[] bytesReceived = new byte[rbytes];
                    Buffer.BlockCopy(state.buffer, 0, bytesReceived, 0, rbytes);
                    state.myQueue.Enqueue(bytesReceived);                
                        client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReceiveCallback_onQuery), state)
                }
    

    【讨论】:

    • 谢谢 MSK!我有那个blockcopy,但我从没想过需要队列!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    相关资源
    最近更新 更多