C# 在C套接字中传递和接收复杂对象
可能重复: 我的复杂对象是IOrderedQueryable类型C# 在C套接字中传递和接收复杂对象,c#,sockets,asynchronous,C#,Sockets,Asynchronous,可能重复: 我的复杂对象是IOrderedQueryable类型 state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); 它有4个属性,都是列表类型 我使用异步套接字通过以下方式发送对象: private void SendDatabaseObj(Socket handler, IOrderedQueryable<BuildHistory1> buildHistoryQueryR
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
它有4个属性,都是列表类型
我使用异步套接字通过以下方式发送对象:
private void SendDatabaseObj(Socket handler, IOrderedQueryable<BuildHistory1> buildHistoryQueryResult)
{
byte[] byteData = ObjectToByteArray(buildHistoryQueryResult);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
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. But how to store?
// 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)
{
//Use the deserializing function here to retrieve the object to its normal form
}
// Signal that all bytes have been received.
receiveDoneQuery.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
我正在接收通过以下方式发送的对象:
private void SendDatabaseObj(Socket handler, IOrderedQueryable<BuildHistory1> buildHistoryQueryResult)
{
byte[] byteData = ObjectToByteArray(buildHistoryQueryResult);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
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. But how to store?
// 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)
{
//Use the deserializing function here to retrieve the object to its normal form
}
// Signal that all bytes have been received.
receiveDoneQuery.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
我的反序列化功能:
private Object ByteArrayToObject(byte[] arrayBytes)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
ms.Write(arrayBytes, 0, arrayBytes.Length);
ms.Seek(0, SeekOrigin.Begin);
Object obj = (Object)bf.Deserialize(ms);
return obj;
}
现在我的问题是接收函数ReceiveCallback\u onQuery。如果要接收更多数据,如何存储以前接收的数据
编辑:
我知道会执行下面的代码,但是是否有其他方法可以将接收到的数据存储在byte[]变量中,以便将它们转换回IOrderedQueryable
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
当流式传输数据时,例如使用套接字时,您必须在协议中内置某种方式,以了解每个消息的开头和结尾在数据中的位置。这是由标准协议的工具(如WCF for SOAP)自动处理的,但如果您要设计自己的协议,则需要自己实现它。最简单的方法是在每条消息之间添加一个已知的分隔符字符或字符串,但需要注意的是,分隔符永远不会出现在消息的数据中。另一种选择是在消息头中发送消息的长度,或者只使用固定长度的消息。不要通过网络传递IQueryable。您不能在另一端使用查询功能。而是创建一个表示请求的类,即包含有关要接收哪些项的信息以及包含匹配对象数组的对象的响应 如果您不想自己处理网络层,并且希望使用比WCF更轻量级的方法,那么可以使用我的Griffin.networking库 我刚刚上传了一段视频,演示了如何在20分钟内创建一个简单的聊天客户端/服务器:
示例代码:您是否考虑过使用像WCF这样现成的解决方案,而不是发明自己的网络协议?请详细说明一下,好吗?WCF是一种通信框架,它使用SOAP协议在服务器上执行远程过程,并传递复杂的数据对象。SOAP是基于XML的,因此它可以轻松处理复杂的数据类型。如果这就是您正在做的事情,那么创建一个WCF服务或一个简单的ASMXWeb服务将比您正在做的容易得多。例如,您可以使用public void SubmitIOrderedQueryable buildHistoryQueryResult这样的方法创建一个WCF服务,然后从客户端应用程序对该服务调用该方法。
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));