简体   繁体   English

在C#中处理套接字服务器发送和接收的数据

[英]Processing data sent and received by a socket server in c#

Hey everyone. 嘿大家。 This place is like a goldmine of knowledge and it's helping me so much! 这个地方就像知识的金矿,对我有很大帮助! My next query is: 我的下一个查询是:

I have byte data being sent to my c# socket server. 我将字节数据发送到我的C#套接字服务器。 I am converting it to an ascii string, then splitting the data based on a common character (like the bar | character) and using the data. 我将其转换为ascii字符串,然后根据常见字符(如bar |字符)分割数据并使用数据。 Typically the first piece of data is a command as a 4 digit number. 通常,第一条数据是一个4位数字的命令。 I can imagine this not being very efficient! 我可以想象这不是很有效! What would be the best way to process the data is an receiving, efficiently? 高效处理接收数据的最佳方法是什么?

Related, how I be trapping and processing commands? 相关,我如何捕获和处理命令? Multiple if statements or a large case/switch statement. 多个if语句或大写的case / switch语句。 I really need speed and efficiency. 我真的需要速度和效率。

Typically the first piece of data is a command as a 4 digit number. 通常,第一条数据是一个4位数字的命令。 I can imagine this not being very efficient! 我可以想象这不是很有效! What would be the best way to process the data is an receiving, efficiently? 高效处理接收数据的最佳方法是什么?

No, converting a number to/from a string is not efficient. 不,将数字转换为字符串或从字符串转换数字效率不高。 But the question is: Do it really matter? 但是问题是:真的重要吗? It sounds to me like you are trying to do premature optimizations. 在我看来,您正在尝试进行过早的优化。 Do not do that. 不要那样做。 Your goal should be to write code that is easy to read and maintain. 您的目标应该是编写易于阅读和维护的代码。 Do not do optimizations until someone actually complains about the performance. 在有人真正抱怨性能之前,不要进行优化。

Related, how I be trapping and processing commands? 相关,我如何捕获和处理命令? Multiple if statements or a large case/switch statement. 多个if语句或大写的case / switch语句。 I really need speed and efficiency. 我真的需要速度和效率。

Again. 再次。 Determine that the command processing really is the bottle neck in your application. 确定命令处理确实是应用程序的瓶颈。

The whole processing really depends on what you do with the incoming messages. 整个处理过程实际上取决于您对传入消息的处理方式。 You provide way to little information to give a proper answer. 您提供了一些信息来给出正确答案的方法。 Create a new question (since two questions in one is not really allowed). 创建一个新问题(因为实际上不允许一个问题中包含两个问题)。 Add code which shows your current handling and describe what you do not like about it. 添加代码以显示您当前的处理方式并描述您对此不满意的地方。

If you really need the performance I guess you shouldn't use a string representation for your command but work directly on the bytes. 如果您确实需要性能,我想您不应在命令中使用字符串表示形式,而应直接在字节上工作。 Four numbers in string format are 32 of 64 bits (depending on which charset you are using) in size, whilst a single byte is sufficient to store a four digit number. 字符串格式的四个数字的大小为32个64位(取决于您使用的字符集),而一个字节就足以存储一个四位数的数字。 Using a lot of branches (which if -statements are) also effects your performance. 使用很多分支( if -statements的话)也会影响您的性能。

My suggestion is that you reserve a fixed size prefix in your message for the command. 我的建议是在命令的消息中保留一个固定大小的前缀。 You then use these bytes to lookup in O(1) in a table which command you should execute, this table can be filled with object that have a method execute . 然后,您可以使用这些字节在应执行命令的表中的O(1)中查找,该表可以填充有具有execute方法的对象。 So you can do something table[command].execute() . 因此,您可以执行table[command].execute()

That being said, I don't think the performance-gain would be that large and that you are better off (maintenance-wise) by using one of the serialization libraries out there. 话虽这么说,我认为性能提升不会那么大,而您可以使用其中的一个序列化库来更好地维护(在维护方面)。

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

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