本文介绍了XmlSerializer.Deserialize块以上的NetworkStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想通过网络流发送XML序列化对象。 我已经用这样的一个UDP广播服务器,它接收来自UDP消息上本地网络。在这里,在服务器端的一个片段: 而(mServiceStopFlag == false)如果为{(mSocket.Available> 0){ IPEndPoint ipEndPoint =新IPEndPoint(IPAddress.Any,DiscoveryPort);字节[] BDATA; //接收发现消息 BDATA = mSocket.Receive(REF ipEndPoint); //处理发现消息 HandleDiscoveryMessage(ipEndPoint.Address,BDATA); ... 这实际上是在客户端: IPEndPoint ipEndPoint =新IPEndPoint(IPAddress.Broadcast,DiscoveryPort); MemoryStream的Mstream工具=新的MemoryStream(); 字节[] BDATA; //创建广播UDP服务器 mSocket =新UdpClient(); mSocket.EnableBroadcast = TRUE; //创建数据报数据的foreach(NetService S IN ctx.Services) XmlHelper.SerializeClass< NetService>(Mstream工具,S); BDATA = mStream.GetBuffer(); //通知服务,而(mServiceStopFlag == FALSE){ mSocket.Send(BDATA,(INT)mStream.Length,ipEndPoint); Thread.sleep代码(DefaultServiceLatency); } 它工作得很好。 但现在i'me试图获得相同的结果,但在一个的TcpClient 插座,而是直接使用了一个的XMLSerializer 实例: 在服务器端: TcpClient的sSocket = k.Key; ServiceContext sContext = k.Value; 信息味精=新的Message(); ,而(sSocket.Connected ==真){如果(sSocket.Available大于0){ StreamReader的TR =新的StreamReader(sSocket.GetStream()); 味精=(消息)mXmlSerialize.Deserialize(TR); //处理消息味精= sContext.Handler(MSG); //与另一个消息如果回复(MSG!= NULL) mXmlSerialize.Serialize(sSocket.GetStream(),味精); }否则 Thread.sleep代码(40); } 和客户端上: 的NetworkStream mSocketStream; 消息rMessage; //网络流 mSocketStream = mSocket.GetStream(); //发送消息 mXmlSerialize.Serialize(mSocketStream,味精); //接收答案 rMessage =(消息)mXmlSerialize.Deserialize(mSocketStream); 回报(rMessage); 将数据发送(可用的属性大于0),但该方法 XmlSerialize.Deserialize (应反序列化消息类)模块。 我缺少什么? 解决方案 当然,因为串行继续读的NetworkStream ,它不会结束时encouter主结束元素。 要达到想要的结果,有必要使用一个的MemoryStream ,其中通知流的末尾当最后一个字节被读取。 I'm trying to sends XML serializable objects over a network stream.I've already used this on an UDP broadcast server, where it receive UDP messages from the local network. Here a snippet of the server side: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); ...Instead this is the client side:IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Broadcast, DiscoveryPort);MemoryStream mStream = new MemoryStream();byte[] bData;// Create broadcast UDP servermSocket = new UdpClient();mSocket.EnableBroadcast = true;// Create datagram dataforeach (NetService s in ctx.Services) XmlHelper.SerializeClass<NetService>(mStream, s);bData = mStream.GetBuffer();// Notify the serviceswhile (mServiceStopFlag == false) { mSocket.Send(bData, (int)mStream.Length, ipEndPoint); Thread.Sleep(DefaultServiceLatency);}It works very fine. But now i'me trying to get the same result, but on a TcpClient socket, but the using directly an XMLSerializer instance:On server side: 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);}And on client side:NetworkStream mSocketStream;Message rMessage;// Network streammSocketStream = mSocket.GetStream();// Send the messagemXmlSerialize.Serialize(mSocketStream, msg);// Receive the answerrMessage = (Message)mXmlSerialize.Deserialize(mSocketStream);return (rMessage);The data is sent (Available property is greater than 0), but the method XmlSerialize.Deserialize (which should deserialize the Message class) blocks.What am I missing? 解决方案 Of course because the serializer continue to read the NetworkStream, and it doesn't ends when encouter the main end element.To achieve the wanted result it's necessary the use of a MemoryStream, which notify the end of stream when the last byte was read. 这篇关于XmlSerializer.Deserialize块以上的NetworkStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 23:50