Reputation: 19
Please help me i am Stuck
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: End of Stream encountered before parsing was completed.
The Above error is comming when executing the method
void ReceiveCallback(IAsyncResult AR)
{
using (ms = new MemoryStream())
{
Socket localsock = (Socket)AR.AsyncState;
int RecLen = localsock.EndReceive(AR);
Array.Resize(ref buffer, RecLen);
bnrFormatter = new BinaryFormatter();
ms.Write(buffer, 0, buffer.Length);
ms.Position = 0;
AddListItem("MS length:" + ms.Length + "\nMS position: " + ms.Position);
TrackerData data = (TrackerData)bnrFormatter.Deserialize(ms);
if (pushToDB(data) == 1)
{
AddListItem("New packet data received from client of IP " + ((IPEndPoint)localsock.LocalEndPoint).Address.ToString() + " and Port " + ((IPEndPoint)localsock.LocalEndPoint).Port + " .");
}
byte[] resBuffer = Encoding.Default.GetBytes("New packet data sent to the server!!");
localsock.BeginSend(resBuffer, 0, resBuffer.Length, SocketFlags.None, new AsyncCallback(EndCallback), localsock);
localsock.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), localsock);
}
}
The Error appers in line
TrackerData data = (TrackerData)bnrFormatter.Deserialize(ms);
Upvotes: 1
Views: 3409
Reputation: 1881
Try this solution. Set the position to 0 of your memory stream and do not use your object but the object type.
ms.Position = 0;
BinaryFormatter bnrFormatter = new BinaryFormatter();
TrackerData data = (TrackerDataObjectType)bnrFormatter .Deserialize(ms);
Upvotes: 0