Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am developing a client/server application in c#.net. Now I am going to pick the communication mechanism use in client/server packets. I want to know, what will be the best format for sending and receiving data on sockets.

Xml serialize or
Binary serialize(i am using Marshalling, demonstrated at i.e. http://msdn.microsoft.com/en-us/library/4ca6d5z7.aspx[^] )

Although Xml serialize will do the work, but as we know, it will be slower. So I think binary marshalling will be the good option. But in this technique, I have to create lot of structs for my each individual packet used in communication. I need help, please suggest if its the only best way to use in this scenerio or there exists some better way to acheive the goal.
Posted
Comments
Sergey Alexandrovich Kryukov 16-Feb-12 3:15am    
Pretty good question, important in some time-critical application; I voted 5.
--SA
BobJanova 16-Feb-12 5:28am    
Both marshalling and BitConverter require that both ends of the connection use the same architecture (in particular, endianness). This is almost always true but just to raise the point.

I'm not sure you can save too much of CPU time on custom marshaling compared to a binary serializer.

However, you can potentially squeeze some extra performance, mostly by reducing redundancy. You can do it if you build you application-level protocol based on specialized and relatively rigid data structures (compared to highly general case handled by serializers), so it won't require you to carry type information (or keep such functionality to a bare minimum). On such a low level, you can effectively serialize separate primitive types and strings using from/to arrays of bytes using the class System.BitConverter, http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 16-Feb-12 15:50pm    
5'ed! :)
>> custom marshaling compared to a binary serializer
You would be amazed :-D
Sergey Alexandrovich Kryukov 16-Feb-12 21:38pm    
You mean, dramatic improvement in performance? I can believe that, but it depends on the case.
Thank you, Espen.
--SA
Muhammad Idrees GS 17-Feb-12 2:12am    
System.BitConverter will only convert primitive types to bytes, not my custom packet structs.
Sergey Alexandrovich Kryukov 19-Feb-12 11:21am    
All custom packet structures are ultimately composed from those primitive type.
Didn't you still get why can you increase performance in principle?
In persistence you decompose structures into primitive types semantically, not through reflection.
--SA
Muhammad Idrees GS 20-Feb-12 0:02am    
Ok, definitely all custom types are composed of primitive types. that's all clear. but I was just looking for more comprehensive sample that could do this trick for my custom structs, I do not want to write code for my each struct individually.
Check out my article fastJSON[^]

Which is faster than XML and Binary serialization and is as robust as Binary for polymorphic data.
 
Share this answer
 
Comments
Muhammad Idrees GS 17-Feb-12 2:20am    
Thanks Mehdi, looking nice. but I am not sure, that JSON will also work with structs. and if it works, do you think that its a better option for convert my custom types to bytes and vice-versa. I mean comparatively with Marshalling in .net.

What will be the difference and what will be the better option :
.Net Marshalling the structs
OR
Json (described in your article)
Mehdi Gholam 17-Feb-12 3:03am    
It really depends on your data, if you are doing a business application then your data will be rich and your serializer will need to be robust enough to support that.
Muhammad Idrees GS 17-Feb-12 4:53am    
I need a robust and efficient mechanism, it will be used in high frequency.
Assuming about thousands of client applications will communicate with server and each packet is passed through this mechanism - serialized into bytes and then back to custom types(.net struct).
I typically write explicit ToBytes/FromBytes* methods on classes I want to transfer. This is pretty fast, and allows you to choose exactly what state information needs to be transferred (any automated serialiser will take everything, and will have to use reflection to find the properties to read) and any compression/transformation that might be useful (for example transferring only 6 entries from a 3×3 transform matrix). However, obviously it requires you to write those methods and keep them up to date.

(*: Actually I tend to use the ByteBuilder and add parameters, using my socket library. But the principle is the same.)

This is not a solution for everyone (particularly if you have lots of pre-existing classes that you want to be transferrable) but it can be a good approach.

Edit: obligatory self-promotion for my sockets library[^].
 
Share this answer
 
v2
Comments
Muhammad Idrees GS 17-Feb-12 2:16am    
there is no any sample that converts struct to bytes. and then bytes back to structs.
BobJanova 17-Feb-12 13:01pm    
Uh, like the solution says, you have to write those methods. They look something like

byte[] ToBytes() {
ByteBuilder bb = new ByteBuilder();
bb.AddInt(id);
bb.AddString(name);
return bb.GetBytes();
}

and

void FromBytes(ByteBuilder data){
id = data.ReadInt();
name = data.ReadString();
}
Muhammad Idrees GS 19-Feb-12 23:59pm    
Thanks Bob

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900