how to serialize an object and sent it out by an UDP packet

carlk

Newcomer
Joined
Mar 12, 2010
Messages
3
Hi,
i'm pretty a noob in .NET and try to serialize a class and send it into an UDP packet.

I use the udpClient class on my client and server sides and I can get an array of byte to transfert from one to another.

What I don't know is how to serialize my object (which basically consist of 3 strings and 1 dictionnary). that object has the name "myCustomClassInstance" in the following example code.

(-->this also means that its size is variable and that I will have to check that the size of my serialized object is under the [MTU minus header] size. but that's another problem.)

For the moment I get to serialize my message by using a binaryformater then send it:

Code:
 Dim mySimpleFormatter As BinaryFormatter = New BinaryFormatter()
 Dim myStream As New MemoryStream()
 mySimpleFormatter.Serialize(myStream, myCustomClassInstance)
 myUdpClient.Send(mystream, mystream.Length)
but on the listining side, the udpClient.receive(myIPEndPoint) method returns a byte array.

Then, I don't get how to convert back that byte array in an instance of the same type of my "myCustomClassInstance" object.


Thanks for the help and advices.
 
yeah I was kind of mixed up because the
Code:
BinaryFormatter..::.Deserialize Method (Stream)
takes a stream as parameter and not a byte()

but I've found that tutorial http://devhood.com/tutorials/tutorial_details.aspx?tutorial_id=448
with a written example and I now understand and make that work.

now I don't understand why I can send a byte array up to 65508 Bytes over the network, because i thought the MTU was generally 1492 Bytes.
 
If you use wireshark, or some other tool to see the packets transferred, you will see a bunch of fragmented, or partial UDP packets. A large array of bytes has to be broken down into multiple UDP packets since, as you say, any one packet will be limited to the MTU size.
 
Back
Top