在c#中处理socket服务器发送和接收的数据
嘿大家。 这个地方就像一个知识的金矿,它对我帮助很大!我的下一个查询是:
我有字节数据发送到我的 C# 套接字服务器。我将其转换为 ascii 字符串,然后根据常见字符(如 bar | 字符)分割数据并使用数据。通常,第一条数据是 4 位数字的命令。我可以想象这不是很有效!有效地处理数据的最佳方式是什么?
相关,我如何捕获和处理命令?多个 if 语句或大型 case/switch 语句。我真的需要速度和效率。
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. 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. Typically the first piece of data is a command as a 4 digit number. 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. I really need speed and efficiency.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,数字与字符串之间的转换效率不高。但问题是:这真的重要吗?在我看来,您似乎正在尝试进行过早的优化。不要那样做。您的目标应该是编写易于阅读和维护的代码。在有人真正抱怨性能之前不要进行优化。
再次。确定命令处理确实是应用程序中的瓶颈。
整个处理实际上取决于您对传入消息的处理方式。您提供的信息很少,无法给出正确的答案。创建一个新问题(因为实际上不允许两个问题合二为一)。添加显示您当前处理方式的代码并描述您不喜欢它的地方。
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.
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.
如果您确实需要性能,我想您不应该使用命令的字符串表示形式,而是直接处理字节。字符串格式的四个数字的大小为 64 位中的 32 位(取决于您使用的字符集),而单个字节足以存储四位数字。使用大量分支(
if
语句就是这样)也会影响你的性能。我的建议是您在消息中为命令保留固定大小的前缀。然后,您可以使用这些字节以 O(1) 的时间在表中查找您应该执行哪个命令,该表可以填充具有方法
execute
的对象。所以你可以做一些table[command].execute()
。话虽如此,我认为性能增益不会那么大,而且使用其中一个序列化库会更好(维护方面)。
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. Using a lot of branches (which
if
-statements are) also effects your performance.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
. So you can do somethingtable[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.