Is Serialization the best for sending data over a socket?

邮差的信 提交于 2021-01-05 06:19:23

问题


Someone told me That Serialization was not the best way to send things over a socket but they said they read that in a book once and was not sure of a better way cause they haven't really done networking before. so is Serialization the best way or is there a better way. Also this is for a game if that makes much of a difference. What i see by searching questions about sending objects over it looks like most people use Serialization but im just checking to see what people thing


回答1:


To serialize basically means to transform into a state appropriate for some type of storage. Data being sent over the network has to be taken out of memory or off a HD and put in some format for being sent. It is ALL serialized.

People may say, serialized to XML or JSON, which is just a very specific TYPE of serialization. Then yes, you can have better or worse ones depending on your needs.




回答2:


In .Net, serialization is handled by the framework, so programming the interface to handle the objects is pretty easy.

If you need to keep a complex object identical from host A to host B, then serialization is useful. Otherwise, if you just need one or two properties from this object to be received by host B and still want to serialize it, you'll need to cast it into a simpler object, and maybe create a new class, which is not a benefit.

In the other hand, not having to write your own socket-to-socket function is maybe safer, considering that your objects won't be altered by the framework serialization methods and classes. In my opinion, if you need to save some bandwidth, using your own socket-to-socket approach is the way. If you opt for security, serialization is the way.




回答3:


I came across your question and in an everlasting quest to educate myself about different topics I decide to do some reading/research. I'm sure you may have come across these things but have you read about Windows Communication Foundation (WCF)? Here's one article that might have some relevance http://blogs.msdn.com/b/carlosfigueira/archive/2008/01/13/writing-custom-requests-to-simple-wcf-services.aspx .

Also in this article Problem with sockets and OutOfMemory error people talk about how they have done socket-based communication but haven't considered WCF previously but it has seemed like somewhat of a blessing. Some people in this article C# Sending multiple objects over same Socket have said that if overhead is very important in this scenario, then it could be an issue however I suppose it's more dependent on what you're trying to accomplish (though you said it is for gaming.)

Again, I wont claim to be an expert I just am intrigued about learning as much as possible and my quick reading from topics relevant to your post have all lead me to WCF. If you're new and curious to this topic it seems like relying on WCF will be easier for you since it has a foundation already in place that you can utilize though probably programming sockets on your own might be faster.

Sorry if I rambled but I hope any of that helps :)



来源:https://stackoverflow.com/questions/9458871/is-serialization-the-best-for-sending-data-over-a-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!