Click here to Skip to main content
15,500,921 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Codeproject!

Let's assume we have a class like so:

public class MovedToPositionPacket : Packet
 public int nx { get; set;}
 public int ny { get; set;}

 public MovedToPositionPacket()

And we then convert this class to a byte array. The byte array then is copied to another byte array(Buffer) with the size of 4096, while the Packet Class is only 96 bytes big.

If we send that byte array(object) how can we convert it back to the same class without using the other 4000 empty bytes?
Ron Beyer 30-Jul-13 15:24pm    
Is there something else in the parent "Packet" class that is needed? Seems like a waste to use 96 bytes to transfer 8 bytes worth of data... And even more of a waste using 4096 bytes to transfer 96 bytes. Can you post the code that converts the "Packet" into a byte array? You should not eat up an entire 3 packets of payload (which is only 1500 bytes per packet incidentally) to transfer such a small amount of data, something is wrong up stream.
Deviant Sapphire 30-Jul-13 15:28pm    
The packets can differentiate from sizes, while a TCP buffer can only hold a specific amount of data.
Ron Beyer 30-Jul-13 15:45pm    
The TCP buffer has a size, which is the MAXIMUM amount of data it can hold, that doesn't mean you have to transmit that much data with each go. Since TCP packets can only hold 1500 bytes each, your 4096 buffer needs 3 packets to send.
Deviant Sapphire 30-Jul-13 17:08pm    
So, assuming I will send one array of 64 bytes. I will have to send another 63 packets of the same size in order to be able to read it? What is up with this protocol?

I am posting this after having seen the Solution you have posted (Solution 3).

I have been involved with specifying my share of binary wire protocols like this through the years. My advice is don't do this unless your requirements call for the smallest communication packets possible. If this is just a program you are creating to learn from, then by all means continue down this path as you will most likely learn a lot from it.

These days there are so many tools that make things like this easier and faster to develop and maintain in the future. I recommend you look into using JSON or XML serialization.[^][^]

Some CodeProject articles:

Soren Madsen
Share this answer
Deviant Sapphire 31-Jul-13 6:44am    
Yes, thanks a lot for your informative answer. I do use XML Serialization, I then convert the string of that into a byte array and with the same method back. :)
How is the receiver going to know what class this data belongs in?? Without some kind of metadata, it's impossible for it to know, so you're going to have to send something else beside 8 bytes.

Seriously, are you trying to come up with your own communication protocol??
Share this answer
Deviant Sapphire 30-Jul-13 17:24pm    
You just made me think about something interesting, thanks a lot, I think this just solved it.
The solution:

I will have to send a byte array in which the two first bytes of the array make up the type of the class(For instance a change of location of the player) and another byte that will tell the server how long the class's byte array is.

For instance

0 = ChangeLocationPacket

Visually, it would look lik this:

( [0] [5 bytes long] [2] [3] [4] [5] [6] )

^ this will be the new byte array, every byte in the array splitted with a [ and a ].

You would then read it out by firstly checking out what type of class it is using either and if or a switch statement:

if(array[0] == 0)
 type = ChangeLocationPacket.GetType();

Then, you would check out how many bytes in length it is:

int Length = (int)array[1];

You could even use two or three bytes to add them up together, or use some magical formula lieing around here in codeproject.

Next, you would read it out, and in this case, there is only one byte which tells how long it is so the code to read it out would be:

Array.Copy(array, 2, newarray, 0, Length);

And afterwards you could return the object and the type in order to convert it back to the class.
Share this answer

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