.Net 使用 Chr() 解析文本

发布于 2024-08-23 23:07:53 字数 429 浏览 4 评论 0原文

我正在构建一个简单的客户端-服务器聊天系统。

客户端将数据发送到服务器,服务器将数据重新发送到所有其他客户端。我使用 TcpListener 和网络流类在客户端和服务器之间发送数据。

我需要发送的字段包括:名称、文本、时间戳等。我使用 ASCII 字符 29 将它们分开。

我还使用 ASCII 字符 30 来标记流数据的结尾。

数据使用 UTF8 编码。

这是一个好方法吗?我会遇到问题吗?有更好的方法吗?

更新:

可能我的问题被误解了,所以我更好地解释一下.. 假设有一个数据列表要从客户端发送到服务器,并且假设仅在一个流中发送所有数据,那么如何发送这些数据?

  • 使用标记
  • 使用字符作为分隔符
  • 对每个字段使用固定长度

I'm building a simple client-server chat system.

The clients send data to the server and the server resends the data to all the other clients. I'm using the TcpListener and Network stream classes to send the data between the client and the server.

The fields I need to send are, for example: name, text, timestamp, etc. I separate them using the ASCII character 29.

I'm also using ASCII character 30 to mark the end of the streamed data.

The data is encoded with UTF8..

Is this a good approach? Will I run into problems? Are there better methods?

UPDATE:

Probably my question was misunderstood, so I explain it better..
Suppose to have a list of data to send from client to server, and suppose to send all the data in only one stream, how do you send these data?

  • Using a markup
  • Using a character as a delimiter
  • Using a fixed length for every fields

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

恬淡成诗 2024-08-30 23:07:53

我发现为单个消息添加长度前缀要简单得多,而不是分隔文本 - 所以而不是写成

message [TERMINATOR]

[LENGTH] message

以二进制形式发送长度,而不是文本 - 要么是固定长度的数字,例如 4 个字节,或者一个可变长度的数字,如果您愿意为了稍微减少传输的数据大小而牺牲简单性的话。)

然后就可以轻松地从流中准确读取正确数量的数据,而不必担心超出范围下一条消息。唯一的缺点是您需要在开始发送消息之前知道消息的长度,但我认为在这种情况下这对您来说不应该成为问题。

顺便说一句,UTF-8 是一种编码,而不是加密算法。

Rather than delimiting text, I find it much simpler to length-prefix individual messages - so rather than writing

message [TERMINATOR]

You'd write

[LENGTH] message

(Send the length in binary, rather than text - either a fixed-length number, e.g. 4 bytes, or a variable-length number if you're willing to sacrifice simplicity for the sake of reducing the transmitted data size very slightly.)

Then it's easy to read exactly the right amount of data from the stream without worrying about overshooting into the next message. The only downside is that you need to know the length of the message before you start sending it, but I don't think it should be a problem for you in this case.

By the way, UTF-8 is an encoding, not an encryption algorithm.

旧竹 2024-08-30 23:07:53

我认为另一种选择可能是序列化/反序列化您的对象并使用 .NET 远程处理。您还可以在发送时加密对象并在接收时解密。

代码项目中,我找到了一个带有源代码的小示例。

I thinkg that another option could be to serialize/deserialize your objects and send/receive them using .NET Remoting. You could also encrypt your objects when sending and decrypt when receiving.

In The Code Project I´ve found a little example with source code.

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