gpt4 book ai didi

c# - StreamReader 不会停止读取 C#

转载 作者:可可西里 更新时间:2023-11-01 02:48:56 27 4
gpt4 key购买 nike

我在两台使用 Windows 窗体的计算机之间建立了 TCP 连接。我有一个整数数组,我正在使用 XmlSerializer 类对其进行序列化,并使用 StreamWriter 发送它并使用 StreamReader 接收它。
发送:

NetworkStream stream = m_client.GetStream();   //m_client is a TCPclient
StreamWriter m_writer = new StreamWriter(stream);
int[] a = new int[3] { 10,20,30 };
XmlSerializer serializer = new XmlSerializer(typeof(int[]));
serializer.Serialize(m_writer, a);
//m_writer.Flush(); //Doesn't help
//m_writer.Close();

在接收端:

TcpClient client = (TcpClient)p_client;
NetworkStream stream = client.GetStream();
StreamReader reader = new StreamReader(stream);
if (stream.CanRead)
{
XmlSerializer serializer = new XmlSerializer(typeof(int[]));
int[] numbers = (int[])serializer.Deserialize(reader);
MessageBox.Show(numbers[0].ToString()); //Is not reached
MessageBox.Show(numbers[1].ToString());
MessageBox.Show(numbers[2].ToString());
}

问题是接收器上的读取器不会停止读取,除非我终止连接或关闭发送器端的 m_writer(在这种情况下我不能用它再次发送)。如果它不停止阅读,下一行(即 MessageBox.Show(numbers[0].ToString()))将不起作用。

我需要关于如何通知接收器在 30 后停止阅读或者它如何理解何时停止阅读的建议?

编辑:
我从How to send integer array over a TCP connection in c#得到了XMLserializer的想法。
我找到了一个需要终止连接的答案,除非这是唯一可能的方法,否则我宁愿不这样做。 XmlSerializer Won't Deserialize over NetworkStream

最佳答案

当通过 TCP 发送数据时,如果字节流中有任何边界,则由您定义和实现它们。

在您的特定示例中,您可以在 XML 数据之前加上字节数似乎是合理的。使用空字节来终止数据也是合理的(因为 XML 本身不应该有空字节)。两种方法都可行,但恕我直言,基于长度的方法更简单(不需要缓冲额外的未处理数据)。

这可能看起来像这样:

NetworkStream stream = m_client.GetStream();   //m_client is a TCPclient
using (MemoryStream buffer = new MemoryStream())
using (StreamWriter writer = new StreamWriter(buffer))
{
int[] a = new int[3] { 10,20,30 };
XmlSerializer serializer = new XmlSerializer(typeof(int[]));
serializer.Serialize(writer, a);

// This code assumes you aren't sending more than 2GB of XML.
// This allows the other end to use int instead of long for the
// length to receive.
byte[] lengthBytes = BitConverter.GetBytes((int)buffer.Length);

stream.Write(lengthBytes, 0, lengthBytes.Length);
buffer.Position = 0;
buffer.CopyTo(stream);
}

然后接收:

TcpClient client = (TcpClient)p_client;
NetworkStream stream = client.GetStream();

// Using BinaryReader is easier than implementing a correct, blocking read
// of fixed numbers of bytes -- TCP can return as little as a single byte for
// any given receive operation, but BinaryReader insulates us from that.
// Leave the stream open so that we can read more later.
using (BinaryReader binary = new BinaryReader(stream, Encoding.UTF8, true))
{
int length = binary.ReadInt32();
byte[] buffer = binary.ReadBytes(length);

using (MemoryStream streamBuffer = new MemoryStream(buffer))
using (StreamReader reader = new StreamReader(streamBuffer))
{
XmlSerializer serializer = new XmlSerializer(typeof(int[]));
int[] numbers = (int[])serializer.Deserialize(reader);
MessageBox.Show(numbers[0].ToString()); //Is not reached
MessageBox.Show(numbers[1].ToString());
MessageBox.Show(numbers[2].ToString());
}
}

(顺便说一句:从技术上讲,根本不需要为 MemoryStream 使用 using,或者根本不需要处理 MemoryStream 对象。但是无论如何,我都使用 using;它给代码增加了很少的开销,并且让我养成了总是处理我创建的流的习惯。

关于c# - StreamReader 不会停止读取 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28560009/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com