Xml反序列化程序 - 意外的文件结尾

时间:2016-04-27 23:08:35

标签: c# xml serialization tcpclient xmlserializer

我正在编写一个服务器 - 许多客户端基于体系结构的application.Server是多线程的 - 每个客户端都有自己的线程来执行通信。我使用TCpClient来执行通信。我一直收到奇怪的错误:当我尝试从流中读取反序列化消息时,意外结束文件丢失了关闭标签。如果消息足够短,则它可以正常工作。我试图让我的缓冲区更大但它没有给出任何结果我很感激任何帮助。

反序列化

char *newptr;
newptr=static_cast<char *>(realloc(ptr, newsize));
if(newptr)
    ptr=newptr;
else {
    // Any treatment you want.  I wrote some fatal failure code, but
    // you might even prefer to go on with current buffer.
    perror("ralloc()");
    exit(1);
}

客户代码

  public static Message GetResponse(string messageString)
    {
        string rootNodeName = String.Empty;
        Message message=null;
        if (String.IsNullOrEmpty(messageString))
        {
            return null;
        }
        Type T = null;
        XmlSerializer serializer = null;

        using (XmlReader xmlReader = XmlReader.Create(new StringReader(messageString)))
        {
            if (xmlReader.MoveToContent() == XmlNodeType.Element)
                rootNodeName = xmlReader.Name;

            T = ReturnMessageType(rootNodeName);
            Contract.Assert(T != null); 
             serializer = new XmlSerializer(T);
        }
        if (serializer != null)
        {
            using (var reader = new StringReader(messageString))
                message = (Message)serializer.Deserialize(reader);
        }


        return message;
    }

服务器代码

  public void PerformCommuncation(Message mes, CancellationToken tocken)
    {
        try
        {
            using (TcpClient client = new TcpClient(Adress, Port))
            {
                string message = String.Empty;

                using (var stream = client.GetStream())
                {
                    if (tocken.IsCancellationRequested)
                    {
                        Console.WriteLine("ZAMYKAM TASKA");
                        return;
                    }

                    var str = Message.Serialize(mes, stream);
                    stream.Flush();

                    byte[] data2 = new byte[client.ReceiveBufferSize];
                    StringBuilder fullReply = new StringBuilder();
                    int numberOfBytesRead = 1;
                  stream.ReadTimeout = 4000;



                    try {
                        while (numberOfBytesRead > 0)

                        {

                            numberOfBytesRead = stream.Read(data2, 0, data2.Length);

                            fullReply.AppendFormat("{0}", Encoding.ASCII.GetString(data2, 0, numberOfBytesRead));
                            if (fullReply.Length == 0) continue;

                        }
                    }
                    catch(Exception)
                    {

                    }

                    var mesagesList = new List<Message>();

                    if (fullReply != null)
                    {

                        var messages = fullReply.ToString().Split((char)23);
                        int size = messages.Length;

                        for (int i = 0; i < size; i++)
                        {

                            var messageType = MessageManager.GetResponse(messages[i]);
                            if (messageType == null)
                                continue;
                            Console.WriteLine();
                            Console.WriteLine("RECEIVED MESSAGE: {0}", messageType.GetType());
                            Console.WriteLine();
                            if (messageType != null)
                            {
                                mesagesList.Add(messageType);
                                Console.WriteLine("DODAJE NA LISTE :{0}", messageType.GetType());
                            }
                        }
                    }
                    if (ComponentType == ComponentType.CommunicationServer && mesagesList.FirstOrDefault(x => x.GetType().ToString().Contains("RegisterResponse")) != null)
                    {
                        mesagesList.Reverse();
                    }
                    foreach (var resp in mesagesList)
                    {
                        dynamic T = this;
                        Message[] messages = null;


                        resp.SenderType = this.ComponentType;
                        resp.Sender = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
                        if (resp.SenderType == ComponentType.CommunicationServer && String.IsNullOrEmpty(((ICommunicationServer)this).MyAdress))
                            ((ICommunicationServer)this).MyAdress = resp.Sender;
                        messages = resp.Run(T);

                        if (messages != null && messages.Length > 0)
                            foreach (var msg in messages)
                            {
                                PerformCommuncation(msg, tocken);
                            }
                    }
                }
            }
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: {0}", e);
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
            // EstablishConnectionAfterCSBreakdown(msg);
            RecreateConnection(mes);
        }

    }

0 个答案:

没有答案