在c#中处理socket服务器发送和接收的数据

发布于 2024-11-06 02:58:47 字数 230 浏览 1 评论 0原文

嘿大家。 这个地方就像一个知识的金矿,它对我帮助很大!我的下一个查询是:

我有字节数据发送到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

格子衫的從容 2024-11-13 02:58:47

通常第一个数据是一个 4 位数字的命令。我可以想象这不是很有效!有效地处理数据的最佳方式是什么?

不,数字与字符串之间的转换效率不高。但问题是:这真的重要吗?在我看来,您似乎正在尝试进行过早的优化。不要那样做。您的目标应该是编写易于阅读和维护的代码。在有人真正抱怨性能之前不要进行优化。

相关,我如何捕获和处理命令?多个 if 语句或大型 case/switch 语句。我真的需要速度和效率。

再次。确定命令处理确实是应用程序中的瓶颈。

整个处理实际上取决于您对传入消息的处理方式。您提供的信息很少,无法给出正确的答案。创建一个新问题(因为实际上不允许两个问题合二为一)。添加显示您当前处理方式的代码并描述您不喜欢它的地方。

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?

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. 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.

老街孤人 2024-11-13 02:58:47

如果您确实需要性能,我想您不应该使用命令的字符串表示形式,而是直接处理字节。字符串格式的四个数字的大小为 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 something 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文