c# – XmlSerializer.通过NetworkStream反序列化块

我正在尝试通过网络流发送
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类)块.

我错过了什么?

最佳答案 当然因为序列化器继续读取NetworkStream,并且在封装主端元素时它不会结束.

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

点赞