Question

I'm writing a network library that utilises both TCP and UDP simultaneously to transfer serialized objects over the internet.

The library (simplified) is split in to a client and a server.

When writing the TCP element, I'm having no trouble. Every time I want to send a serialized object, I simply send the size of that incoming object ahead of the object itself, and wait until the NetworkStream has enough bytes queued up.

However, writing a UDP server is a bit harder. I have a question:

When doing:

data = udpClient.Receive(ref sender);

I'm not sure what to expect here on the server end. In this first example, I will assume that no data is lost or ordered wrongly. But what happens if the objects I've serialized are split in to multiple packets, and come from multiple clients? For example, if these two events happen simultaneously:

  • Client A sends object of 10,000 bytes
  • Client B sends object of 12,000 bytes

Then will I receive something like this, in this order (or near enough)?

  • Client A object part 1/5
  • Client B object part 1/6
  • Client A object part 2/5
  • Client B object part 2/6
  • etc...

If so, should I just store the data I receive somewhere for each client until I think I have enough, and then attempt to deserialize it?

Thank you

Was it helpful?

Solution

By the time you've added part ordering and splitting, you're quite a long way down the road of reinventing TCP - but doing so yourself instead of using the hard-won experience of many years.

Fundamentally it sounds like UDP isn't really suitable for what you're trying to do. Is it an absolute requirement?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top