当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Xml Deserializer - unexpected end of file

问题描述:

I am writing a server - to many clients architecture based application.Server is multithread - each client has its own thread where communication is performed .I use TCpClient to perform communication . I keep getting strange error :unexpected end of file occured missing close tags" when i try to deserialize message from read from stream . If message is short enough it works corectly. I tried to make my buffers bigger but it didin't give any result . I would appreciate any help.

Deserialization

 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;

}

Client code

 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);

}

}

Server code

 private void ThreadProc(object obj)

{

String data = String.Empty;

{

using (var client = (TcpClient)obj)

{

using (var stream = client.GetStream())

{

byte[] data2 = new byte[client.ReceiveBufferSize];

StringBuilder fullReply = new StringBuilder();

int numberOfBytesRead = 2;

stream.ReadTimeout = 1000;

try {

do

{

if (numberOfBytesRead <= 1) continue;

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

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

if (fullReply.Length == 0) continue;

log.Info("Odebrano wiadomość");

} while (stream.DataAvailable);

}

catch(Exception)

{ Console.WriteLine(" EXCEPTION ON read !!!"); };

var mesagesList = new List<Message>();

{

var messageString = fullReply.ToString();

var messageType = MessageManager.GetResponse(messageString);

dynamic T = this;

if (messageType != null)

{

Console.WriteLine("ODEBRANO: ", messageType);

messageType.SenderType = ComponentType.CommunicationServer;

messageType.Sender = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();

var response = messageType.Run(T);

log.Info("SERWER ODPOWIADA: ");

if (response != null)

mesagesList.AddRange(response);

}

}

for(int i=0;i<mesagesList.Count;i++)

{

var r = mesagesList[i];

if (r == null) continue;

XmlSerializer serializer = new XmlSerializer(r.GetType());

try

{

serializer.Serialize(stream, r);

stream.Flush();

}

catch (Exception e)

{

serializer.Serialize(stream, r);

}

if (mesagesList.Count > 1 && i!=mesagesList.Count-1)

stream.Write(new byte[] { 23 }, 0, 1);

}

}

}

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: