Occasionally get an error "Multiple root elements when deserializing data" on server side.

  • Thread starter Thread starter Pomegranate Tree
  • Start date Start date
P

Pomegranate Tree

Guest
The client sends serialized stream data that contains an instance of a customized class, mainly to send JPG data, it basically works fine when both client and server <g class="gr_ gr_638 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="638" id="638">winforms</g> are running on the same machine, but occasionally throws an exception about "multiple root elements", and the case is much worse when the server <g class="gr_ gr_698 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="698" id="698">winform</g> runs on a remote WinServer 2012.

//////////Here's the function on the Server side that brought the exception:

private void Receive(object clientObj)
{
Socket clientSocket = clientObj as Socket;
IPEndPoint clientPoint = (IPEndPoint)clientSocket.RemoteEndPoint;
try
{
int clientDataLen = clientSocket.Receive(clientData);
MemoryStream mStream = new MemoryStream();

mStream.Write(clientData, 0, clientDataLen);
mStream.Position = 0;
MetaData metaFile = serializer.ReadObject(mStream) as MetaData; //DataContractSerializer serializer

mStream.Flush();
mStream.Close();

OnMsgReceived(clientSocket, clientPoint, metaFile); //OnMsgReceived is just an event that shares data
Receive(clientSocket);
}

//////////The clientData (and metaFile) is the following MetaData class which exists in both Server and Client side:

[DataContract(Namespace = "app2watch")]
[KnownType(typeof(Bitmap))]
class MetaData
{
[DataMember]
public int Type { get; set; }
[DataMember]
public string Text { get; set; }
[DataMember]
public int Integer { get; set; }
[DataMember]
public float Float { get; set; }
[DataMember]
public bool BoolFlag { get; set; }
[DataMember]
public Image ScreenFrame { get; set; }
[DataMember]
public Image MonitorLayout { get; set; }
}


//////////Here's the function on the client side that serializes and sends the MetaData:

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
MetaData metaFile = new MetaData();
if (monitorLayoutUpdated == true)
{
metaFile.MonitorLayout = myArt.CreateLayoutImg(); //metaFile gets one image
monitorLayoutUpdated = false;
}
metaFile.ScreenFrame = myArt.CaptureRegionImg(); //metaFile gets another image

MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, metaFile);
clientSocket.Send(ms.GetBuffer(), (int)ms.Length, SocketFlags.None);
ms.Flush();
ms.Close();

}

Do I need to provide additional info? Could anyone tell me what is a root element?!

Given the fact that the error occurs only occasionally and less often on a PC with better performance (especially more RAM), could it be a cause that the client sends the data too frequently? Currently, it does that every 50 milliseconds, but it doesn't help a lot when I changed that.

Thank you very much.

Continue reading...
 
Back
Top