Advertisement

best way to send an receive multivarible data by socket

Started by December 13, 2014 08:49 AM
10 comments, last by Nypyren 9 years, 11 months ago

you just can set some varibales into an string


Several suggestions above indicated why this is a bad idea. For example, string manipulation in C# generate lots of garbage, and the string encoded values will use more bandwidth (as they are longer than equivalent binary encoded values.)
enum Bool { True, False, FileNotFound };

...BinaryFormatter...


I would recommend *against* using BinaryFormatter for a couple major reasons:

- There is a lot of per-serialization overhead (in the output data) that will DRAMATICALLY waste bandwidth unless your object graphs are large.
- The BinaryFormatter class itself is missing from most frameworks other than desktop .Net and Mono (in particular: WP8, WinRT and 360 cannot use it).


You can write your own reflection-based serializer, but it has a performance impact, and is kind of difficult to get it to handle all of the possible types that you might want to serialize.

The main alternative is using BinaryReader/BinaryWriter and a common interface on classes that use it. This is much more efficient at runtime but more tedious for the programmer (less tedious than dealing with the string-oriented approaches above, since you don't have to parse the values from strings back into numbers and error-check them first). The big thing to be aware of when using binary data transfer is byte ordering (endianness). This is pretty simple to deal with - you can create your own class that does what the BinaryReader/Writer pair does that keeps it consistent no matter what the host endianness is.

This topic is closed to new replies.

Advertisement