This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18725|Reply: 0

[Source] One question about serialization and deserialization

[Copy link]
Posted on 10/27/2015 7:07:26 PM | | | |
Yesterday when I was doing database synchronization testing, I used a bit of serialization and deserialization technology. I abstract the fields of a table into an entity class DiagramInfo, then store the new records of the client in a generic collection, and then serialize this generic collection and send it to the server through TCP, which deserializes it, and then writes this data to the database. This is probably the process of this operation.

       Serialization I use the System.Runtime.Serialization.Iformatter class to serialize generic collections into binary streams. Some of the serialized code is as follows:

  1. private void Seriable(List diagrams)
  2.        {

  3.             MemoryStream stream = new MemoryStream();

  4.             IFormatter formater = new BinaryFormatter();

  5.             formater.Serialize(stream, diagrams);

  6.             stream.Flush();

  7.             serializedData = stream.ToArray();

  8.             stream.Close();

  9.         }
Copy code
Then send using the TCP protocol, this part is omitted.

The server partially receives the data, saves it as a MemoryStream, and then deserializes it, part of the code is as follows:
  1. do
  2. {

  3.       bytesRead = stream.Read(responseBuffer, 0, bufferSize);

  4.       memstream.Write(responseBuffer, 0, bytesRead);

  5. }

  6. while (bytesRead > 0);

  7. IFormatter formater = new BinaryFormatter();

  8. diagrams = (List)formater.Deserialize(memstream);

  9. stream.Close();

  10. memstream.Close();

  11. client.Close();
Copy code
At this time, run the client and server to synchronize the data, and the following error message appears:
Error behavior: diagrams = (List)formater. Deserialize(memstream);
Literally: encountered the end of the stream before the conversion was complete.
2. SolutionAfter a Google search, someone answered the following error possibilities:
1. The types of serialization and deserialization are different, which can be excluded here;
2. There is no buffer to clear the stream during serialization, and the Flush() method is not used, which can also be excluded here.
3. The buffer size of the receiving process before deserialization may not be enough, I think there should be no problem with this, deserialization is carried out after the acquisition stream, it should have nothing to do with the size of the buffer, personal opinion, I have not tried it;
4. Add before deserializationstrem. Position=0。 (Stream is the stream to be deserialized, actually memstream)
After testing, the fourth may work for my program. So why is this happening, after a single step of debugging, observe the memstream. Postion's value changes, and it is found that every time a write operation is performed, the value points to the end, which explains why the program reports such an error. But what I don't understand is why the deserialization function (Deserialize) instead of deserializing the entire stream by default, starting from where the Positon is?





Previous:String[3]: the Size property has an invalid size of 0.
Next:vs2010 connection server vss2005 time error, the problem is as shown in the figure, ask for the god's help
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com