如何将一个巨大的字符串写入 NetworkStream?

发布于 2024-09-12 02:21:34 字数 836 浏览 2 评论 0原文

从互联网上,我找到了从 NetworkStream 读取巨大字符串的方法。

 static NetworkStream ns = null;
 static StringBuilder sb = null;
 static byte[] buffer = null;
 static int position = 0;
 //.......................................
 //other codes skipped for simplicity
 //....................................... 
 private static string Read()
 {
        if (ns.CanRead)
        {
            sb.Clear();
            position = 0;
            while (ns.DataAvailable)
            {
                position = ns.Read(buffer, 0, buffer.Length);
                sb.Append(Encoding.Unicode.GetString(buffer, 0, position));
            }

            return sb.ToString().Trim();
        }
        else
        {
            return null;
        }
 }

但是,我找不到如何将大字符串写入 NetworkStream 的示例。

写作是否存在与阅读一样的“对称”模式?

先感谢您。

From the internet I got the way to read a huge string from a NetworkStream.

 static NetworkStream ns = null;
 static StringBuilder sb = null;
 static byte[] buffer = null;
 static int position = 0;
 //.......................................
 //other codes skipped for simplicity
 //....................................... 
 private static string Read()
 {
        if (ns.CanRead)
        {
            sb.Clear();
            position = 0;
            while (ns.DataAvailable)
            {
                position = ns.Read(buffer, 0, buffer.Length);
                sb.Append(Encoding.Unicode.GetString(buffer, 0, position));
            }

            return sb.ToString().Trim();
        }
        else
        {
            return null;
        }
 }

However, I cannot find an example how to write a huge string to a NetworkStream.

Is there a "symmetrical" pattern for writing as we do for reading?

Thank you in advance.

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

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

发布评论

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

评论(1

若能看破又如何 2024-09-19 02:21:34

这种阅读代码在很多方面都是危险的错误:

  • 以这种方式使用静态变量,它绝对不适合多线程任务。 (我希望这只是由于您简化了它......)
  • 它永远不会将变量初始化为非空值 - 再次,希望这不是真正的代码
  • 它使用 DataAvailable 属性来决定何时应该初始化被“完成” - 这是非常危险的,因为这意味着如果数据包在流中延迟,您可以读取预期数据的一半 它
  • 始终使用 Encoding.Unicode ,这很少是最好的编码的选择
  • 它假设它将始终读取整数个字符。如果一个字符在读取时被分割怎么办?这就是 Encoder/Decoder 类的用途...但您实际上并不需要在这里使用它们 - 见下文。

我强烈建议您将 NetworkStream 包装在 StreamReader 中进行读取,将 StreamWriter 包装在 StreamWriter 中进行写入。这就是他们的目的。然后,您可以一次读取一行,或者只是读取一个 char[] 缓冲区,或者读取到流的末尾(这意味着“直到套接字关闭”)。这对于纯文本协议来说很好。

如果你有一个混合文本和二进制数据的协议,生活就会变得更加困难。就我个人而言,我喜欢对消息进行长度前缀的协议 - 这样您就可以读取您想要的数据,然后执行您想要的任何转换。

无论如何,我希望这些随机选择的想法有所帮助......如果您需要更详细的帮助,请提供您正在使用的协议的详细信息。

That reading code is dangerously wrong in many ways:

  • By using static variables in this way, it's hopelessly unsuitable for multi-threaded tasks. (I hope that's just due to you simplifying it...)
  • It never initializes the variables to non-null values - again, hopefully that's not the real code
  • It uses the DataAvailable property to decide when it should be "done" - that's incredibly dangerous as it means if a packet is delayed in the stream, you could read half as much data as you expected to
  • It uses Encoding.Unicode always, which is rarely the best choice of encoding
  • It assumes that it will always read a whole number of characters. What if one character is split between reads? That's what the Encoder/Decoder classes are for... but you don't really need to use them here anyway - see below.

I would strongly suggest that you wrap the NetworkStream in a StreamReader for reading, and a StreamWriter for writing. That's what they're for. You can then read a line at a time, or just a char[] buffer, or to the end of the stream (which means "until the socket is closed"). This is fine for a text-only protocol.

If you've got a protocol which mixes text and binary data, life becomes a lot harder. Personally I like protocols which length-prefix messages - that way you can read only the data you're meant to, and then perform whatever conversion you want.

Anyway, I hope this random selection of thoughts helps... if you want more detailed assistance, please provide details of what protocol you're using.

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