StreamReader不会停止读取C#

时间:2015-02-17 10:38:08

标签: c# winforms sockets tcp

我在使用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

2 个答案:

答案 0 :(得分:1)

只有当底层套接字关闭时,

NetworkStream才会结束。

因此您需要根据需要阅读数据;从中创建一个MemoryStream并使用它进行反序列化,如链接帖子所示。

答案 1 :(得分:1)

通过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());
    }
}

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