简体   繁体   English

C#UDP对象二进制序列化与大对象

[英]C# UDP Object Binary Serialization with large objects

I'm writing a network library that utilises both TCP and UDP simultaneously to transfer serialized objects over the internet. 我正在编写一个同时利用TCP和UDP来通过互联网传输序列化对象的网络库。

The library (simplified) is split in to a client and a server. 库(简化)分为客户端和服务器。

When writing the TCP element, I'm having no trouble. 在编写TCP元素时,我没有遇到任何麻烦。 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. 每次我想发送一个序列化对象时,我只是在对象本身之前发送该传入对象的大小,并等到NetworkStream有足够的字节排队。

However, writing a UDP server is a bit harder. 但是,编写UDP服务器有点困难。 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 客户端A发送10,000字节的对象
  • Client B sends object of 12,000 bytes 客户端B发送12,000字节的对象

Then will I receive something like this, in this order (or near enough)? 那么我会按此顺序(或接近足够)接收这样的东西吗?

  • Client A object part 1/5 客户端对象部分1/5
  • Client B object part 1/6 客户B对象部分1/6
  • Client A object part 2/5 客户端对象部分2/5
  • Client B object part 2/6 客户B对象部分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 谢谢

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. 当你添加零件订购和拆分时,你在重塑TCP的道路上还有很长的路要走 - 但这样做是自己而不是使用多年来来之不易的经验。

Fundamentally it sounds like UDP isn't really suitable for what you're trying to do. 从根本上说,它听起来就像UDP是不是真的适合你想要做什么。 Is it an absolute requirement? 这是绝对的要求吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM