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
3 Answers
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.

- 730,956
- 141
- 904
- 1,278

- 2,708
- 4
- 22
- 35
-
What i normally use is the BinaryFormatter is that the best one for sending structs oever a socket? looking for speed. – Shredder2500 Feb 27 '12 at 01:16
-
Yeah if your sole goal is speed of transmission. Binary as you are doing it will be the best. – Jordan Feb 27 '12 at 01:22
-
@Jordan Binary IS the best, fastest way of doing it, however BinaryFormatter (.NET specific) uses Reflection, and therefore has more overhead than writing your own serialization logic using BinaryWriter. – bbosak Feb 27 '12 at 01:26
-
@IDWMaster I completely agree with you. However since performance outside of socket transmission didn't appear to even be an issue I figured it wasn't really a salient point but I would personally write my own. For me performance is always an issue in all elements. Perhaps for the OP it is simply easier to use BinaryFormatter. – Jordan Feb 27 '12 at 01:32
-
do you have a link that you can put to get me started on writing my own? – Shredder2500 Feb 27 '12 at 01:41
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.

- 2,701
- 2
- 40
- 67
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 :)

- 1
- 1

- 769
- 1
- 8
- 26
-
Thanks for you input, I do know about WCF but i was just mainly wanting to know because me and a friend was talking about c# and sockets and how one of my programs send stuff over. when he saw i used Serialization he said he read once its not the best way to do it, but i never heard of another way of doing it so thats why i asked – Shredder2500 Feb 27 '12 at 01:45
-
Hey there, thank you for your reply. Sorry if my response was not exactly what you were looking for. I'm certainly no expert on the issue but would like to know more about it as well so I've been doing more reading. I came across some more links that, while I cannot say it is the best practice, might be helpful: http://stackoverflow.com/questions/284885/multi-client-async-sockets-in-c-best-practices and (for sockets vs. WCF) http://stackoverflow.com/questions/1040278/sockets-vs-wcf . – Stephen Tetreault Feb 27 '12 at 01:51
-
Thank you so much for your links, If you want to discuss more stuff about c# so we can both learn more i have a skype so we dont have a forever size comments, just let me know – Shredder2500 Feb 27 '12 at 01:54