XmlSerializer。通过NetworkStream反序列化块

时间:2010-04-29 18:06:23

标签: c# xml-serialization network-protocols

我正在尝试通过网络流发送XML可序列化对象。

我已经在UDP广播服务器上使用它,它从本地网络接收UDP消息。这是服务器端的一小部分:

while (mServiceStopFlag == false) {
    if (mSocket.Available > 0) {
        IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, DiscoveryPort);       byte[] bData;

    // Receive discovery message
    bData = mSocket.Receive(ref ipEndPoint);
    // Handle discovery message
    HandleDiscoveryMessage(ipEndPoint.Address, bData);
    ...

相反,这是客户端:

IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Broadcast, DiscoveryPort);
MemoryStream mStream = new MemoryStream();
byte[] bData;

// Create broadcast UDP server
mSocket = new UdpClient();
mSocket.EnableBroadcast = true;

// Create datagram data
foreach (NetService s in ctx.Services)
    XmlHelper.SerializeClass<NetService>(mStream, s);
bData = mStream.GetBuffer();

// Notify the services
while (mServiceStopFlag == false) {
    mSocket.Send(bData, (int)mStream.Length, ipEndPoint);
    Thread.Sleep(DefaultServiceLatency);
}

它非常好用。

但是现在我想尝试获得相同的结果,但是在TcpClient套接字上,但直接使用XMLSerializer实例:

在服务器端:

    TcpClient sSocket = k.Key;
ServiceContext sContext = k.Value;
Message msg = new Message();

while (sSocket.Connected == true) {
    if (sSocket.Available > 0) {
        StreamReader tr = new StreamReader(sSocket.GetStream());
        msg = (Message)mXmlSerialize.Deserialize(tr);

        // Handle message
        msg = sContext.Handler(msg);
        // Reply with another message
        if (msg != null)
            mXmlSerialize.Serialize(sSocket.GetStream(), msg);
    } else
        Thread.Sleep(40);
}

在客户端:

NetworkStream mSocketStream;
Message rMessage;

// Network stream
mSocketStream = mSocket.GetStream();

// Send the message
mXmlSerialize.Serialize(mSocketStream, msg);
// Receive the answer
rMessage = (Message)mXmlSerialize.Deserialize(mSocketStream);

return (rMessage);

发送数据(Available属性大于0),但方法XmlSerialize.Deserialize(应该反序列化Message类)阻塞。

我错过了什么?

1 个答案:

答案 0 :(得分:2)

当然因为序列化程序继续读取NetworkStream,并且在封装主结束元素时它不会结束。

为了达到想要的结果,必须使用MemoryStream,它在读取最后一个字节时通知流的末尾。