首先,讓我解釋我的情況:我正在使用套接字進行通信的C#中的客戶端和服務器上工作。從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);
}
}
問題可能是發件人代碼。你能發佈發送對象的代碼嗎?確保在每個對象發出後刷新套接字 – GETah
@GETah +1具有相同的想法和更多細節。 – Mualig