首先,让我解释一下我的情况:我正在使用 C# 开发客户端和服务器,它们使用套接字进行通信。
出于实际原因,我使用两个套接字的异步部分将二进制序列化对象从客户端传输到服务器,反之亦然。
我的问题是,当我一次发送太多对象时,接收对象“堆栈”到缓冲区中,当我尝试反序列化缓冲区内容时,它只给我一个对象。
我的问题是:如何将每个对象与缓冲区分开?
这是我的ReceiveCallback
功能:
private void ReceiveMessageCallback(IAsyncResult asyncResult)
{
Socket socket = (Socket)asyncResult.AsyncState;
try
{
int read = socket.EndReceive(asyncResult);
if (read > 0)
{
Log("Reception of " + read + " Bytes");
// Jumper is an object that I use to transport every message
Jumper pod = Common.Serializer.DeSerialize<Jumper>(this.readbuf);
Buffer.SetByte(this.readbuf, 0, 0);
socket.BeginReceive(this.readbuf, 0, this.readbuf.Length, SocketFlags.None, new AsyncCallback(ReceiveMessageCallback), socket);
//We fire an event to externalise the analyse process
Receiver(pod, socket);
}
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == System.Net.Sockets.SocketError.ConnectionReset)
{
socket.Close();
Log("Distant socket closed");
}
else
Log(ex.Message);
}
catch (Exception ex)
{
Log(ex.Message);
}
}