如何将一个巨大的字符串写入 NetworkStream?
从互联网上,我找到了从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种阅读代码在很多方面都是危险的错误:
DataAvailable
属性来决定何时应该初始化被“完成” - 这是非常危险的,因为这意味着如果数据包在流中延迟,您可以读取预期数据的一半 它Encoding.Unicode
,这很少是最好的编码的选择Encoder
/Decoder
类的用途...但您实际上并不需要在这里使用它们 - 见下文。我强烈建议您将
NetworkStream
包装在StreamReader
中进行读取,将StreamWriter
包装在StreamWriter
中进行写入。这就是他们的目的。然后,您可以一次读取一行,或者只是读取一个char[]
缓冲区,或者读取到流的末尾(这意味着“直到套接字关闭”)。这对于纯文本协议来说很好。如果你有一个混合文本和二进制数据的协议,生活就会变得更加困难。就我个人而言,我喜欢对消息进行长度前缀的协议 - 这样您就可以仅读取您想要的数据,然后执行您想要的任何转换。
无论如何,我希望这些随机选择的想法有所帮助......如果您需要更详细的帮助,请提供您正在使用的协议的详细信息。
That reading code is dangerously wrong in many ways:
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 toEncoding.Unicode
always, which is rarely the best choice of encodingEncoder
/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 aStreamReader
for reading, and aStreamWriter
for writing. That's what they're for. You can then read a line at a time, or just achar[]
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.