问题描述
编辑:
A什么我试图在这里做的,并与
我使用C#异步套接字来从源接收数据。
我的问题是如何以及在何处存储接收到的数据,如果有更多的要接收?
当接收到一个字符串,我可以只使用一个字符串生成器接收并存储就像这样从MSDN:
私人无效ReceiveCallback_onQuery(IAsyncResult的AR)
{
尝试
{
//检索状态对象和客户端套接字
//从异步状态对象。
StateObject状态=(StateObject)ar.AsyncState;
客户端的Socket = state.workSocket; //从远程设备读取数据。
INT读取动作= client.EndReceive(AR); 如果(读取动作大于0)
{
//可能有更多的数据,所以存储接收到的数据为止。
dataReceived + = state.buffer; //无效(dataReceived为byte []类型) //获取数据的其余部分。
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
新的AsyncCallback(ReceiveCallback_onQuery),状态);
}
其他
{
//所有数据已经到达;把它放在响应。
如果(dataReceived→1)
{
response_onQueryHistory = ByteArrayToObject(dataReceived)
}
//信号,即所有字节已收到。
receiveDoneQuery.Set();
}
}
赶上(例外五)
{
Console.WriteLine(e.ToString());
}
}
我不希望我接收到的数据转换成字符串作为我来说,我收到了一个复杂的对象。
发送的数据是序列化,我还罚款与反序列化。
我的问题如何不断地接收来自插座的数据,而无需使用字符串生成器来存储它。
谢谢!
我的问题是如何以及在何处存储接收到的数据,如果有更多的要接收?
可以使用Buffer.BlockCopy和队列起来,对于如
INT rbytes已= client.EndReceive(AR); 如果(rbytes已> state.buffer)
{
字节[] =与BytesReceived新的字节[rbytes已]
Buffer.BlockCopy(state.buffer,0,与BytesReceived,0,rbytes已);
state.myQueue.Enqueue(与BytesReceived);
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
新的AsyncCallback(ReceiveCallback_onQuery),州)
}
EDIT:A more concise explanation of what I was trying to do here and with answers
I'm using c# asynchronous sockets to receive data from a source.
My problem is how and where to store received data if there are more to be received?
When a string is received, I can just use a string builder to receive and store just like this from 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());
}
}
I dont want to convert my received data into a string as in my case, I am receiving a complex object.
The data sent was serialized and I'm also fine with deserializing.
My question how to 'continually' receive data from the socket without using the string builder to store it.
Thanks!
My problem is how and where to store received data if there are more to be received?
Can use Buffer.BlockCopy and queue it up, for eg,
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)
}
这篇关于如何异步接收复杂的对象在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!