StreamReader 不会停止读取 C#

StreamReader does not stop reading C#

我使用 windows 形式在两台计算机之间建立了 TCP 连接。我有一个整数数组,我正在使用 XmlSerializer class 对其进行序列化,并使用 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());
}

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

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

编辑:
我从
I found an answer which requires terminating the connection, which I'd rather not do unless it is the only way possible.

那里得到了 XMLserializer 的想法

NetworkStream 仅当底层套接字关闭时才会结束。

所以你需要多少就读多少资料;从中创建一个 MemoryStream 并使用它进行反序列化,如链接 post.

中所示

当通过 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无论如何;它给代码增加了很少的开销,并且让我养成了总是处理我创建的流的习惯。