在TCP套接字中通信二进制数据 [英] Communicate binary data in TCP sockets

查看:427
本文介绍了在TCP套接字中通信二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在c#.net中开发客户端/服务器应用程序.现在,我将选择客户端/服务器数据包中使用的通信机制.我想知道,什么是在套接字上发送和接收数据的最佳格式.

Xml序列化或
二进制序列化(我正在使用编组,请参见 http://msdn.microsoft.com/en-us /library/4ca6d5z7.aspx [ ^ ])

虽然Xml序列化将完成这项工作,但众所周知,它会慢一些.因此,我认为二进制编组将是一个不错的选择.但是在这种技术中,我必须为我在通信中使用的每个单独的数据包创建很多结构.我需要帮助,请提出建议,如果这是在此场景中使用的唯一最佳方法,或者是否存在实现目标的更好方法.

解决方案

我不是确保与二进制序列化程序相比,可以在自定义封送处理上节省过多的CPU时间.

但是,您可能会挤占一些额外的性能,这主要是通过减少冗余来实现的.如果您是基于专用且相对严格的数据结构(与序列化程序处理的高度通用的情况相比)构建应用程序级协议的,则可以这样做,因此它不需要您携带类型信息(或将此类功能保留在最低限度).在如此低的级别上,您可以使用类System.BitConverter fastJSON [ ^ ]

对于多态数据,它比XML和二进制序列化要快,并且与二进制一样健壮.


我通常在要传输的类上编写显式的ToBytes/FromBytes *方法.这非常快,可以让您准确选择需要传输的状态信息(任何自动序列化器都会处理所有信息,并且必须使用反射来查找要读取的属性)以及任何可能有用的压缩/转换(对于例如,仅从3×3转换矩阵传输6个条目).但是,显然,您需要编写这些方法并使它们保持最新.

(*:实际上,我倾向于使用ByteBuilder并通过我的套接字库添加参数.但是原理是相同的.)

这不是每个人的解决方案(特别是如果您有很多想要迁移的既有类),但这可能是一个很好的方法.

我的套接字库的强制性自我推广 [ ^ ].

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.

解决方案

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


Check out my article fastJSON[^]

Which is faster than XML and Binary serialization and is as robust as Binary for polymorphic data.


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[^].


这篇关于在TCP套接字中通信二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆