2013-04-18 36 views
0

我的想法 - >客戶端服務器系統通過TCP套接字交換文本消息(字符串)。我希望客戶端和服務器之間的協議基於XML。而且因爲套接字之間的信息是作爲byte發送的,所以我必須施放。所以這裏是我所做的: 類別TheMessagestring類型的財產。我將該類的對象與字符串作爲對象的屬性發送,並使其從Objectbyte[]XmlSerialization。另一方面,我採用反向工程。 這是我如何序列化和發送從客戶端到服務器:通過套接字對象的XML序列化

msg.Message = Console.ReadLine(); 
byte[] writeBuff = XmlRefacrotClient.ObjectToByteArray(msg); 
Stream stm = client.GetStream(); 
stm.Write(writeBuff, 0, writeBuff.Length); 

這是我使用的序列化方法:

public static byte[] ObjectToByteArray(TheMessage obj) 
{ 
    try 
    { 
     MemoryStream ms = new MemoryStream(); 
     XmlSerializer xmlS = new XmlSerializer(typeof(Message.TheMessage)); 
     XmlTextWriter xmlTW = new XmlTextWriter(ms, Encoding.UTF8); 

     xmlS.Serialize(xmlTW, obj); 
     ms = (MemoryStream)xmlTW.BaseStream; 

     return ms.ToArray(); 
    } 
    catch(Exception) 
    { 
     throw; 
    } 
} 

這是我收到的數據在服務器端:

byte[] readBuff = new byte[1024]; 
s.Receive(readBuff); 
String str = (XmlRefactorServer.ByteArrayToObject(readBuff)).ToString(); 

Console.WriteLine(str); 

這是反序列化的方法:

public static Object ByteArrayToObject(byte[] arr) 
{ 
    try 
    { 
     XmlSerializer xmlS = new XmlSerializer(typeof(Message.TheMessage)); 
     MemoryStream ms = new MemoryStream(); 
     XmlTextWriter xmlTW = new XmlTextWriter(ms, Encoding.UTF8); 

     return xmlS.Deserialize(ms); 
    } 
    catch(Exception) 
    { 
     throw; 
    } 
} 

一切運行平穩,直到ByteArrayToObjectreturn method.I與上return xmlS.Deserialize(ms);行描述There is an error in XML document (0, 0).得到InvalidOperationException

有什麼建議嗎?

+0

你採取了字節數組並將其轉換爲字符串或東西,看看它看起來像什麼?也許你可以發現XML中的錯誤,如果你看到它的樣子。 – guysherman

+0

我首先通過轉換string - > byte array - > string(來測試連接本身)並且它工作正常。我在雙向接收和發送消息。 – Milkncookiez

回答

0

試試這個。這是一個更清潔:

//--------------------------------------------------------------------- 
     public static Byte[] ObjectToByteArray<T>(T obj) 
     { 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       XmlSerializer xmlS = new XmlSerializer(typeof(T)); 
       xmlS.Serialize(ms, obj); 

       return ms.ToArray(); 
      } 
     } 
     //--------------------------------------------------------------------- 
     public static T ByteArrayObject<T>(Byte[] bObj) 
     { 
      using (MemoryStream ms = new MemoryStream(bObj)) 
      { 
       XmlSerializer xmlS = new XmlSerializer(typeof(TheMessage)); 
       return (T)xmlS.Deserialize(ms); 
      } 
     } 
     //--------------------------------------------------------------------- 
     public static void Sending(Byte[] bData) 
     { 
      Stream stm = client.GetStream(); 

      // always write size first when using TCP 
      Byte[] bSize = BitConverter.GetBytes(bData.Length); 
      stm.Write(bSize, 0, bSize.Length); 
      stm.Write(bData, 0, bData.Length); 
     } 

     //--------------------------------------------------------------------- 
     public static void Receiving(Byte[] bData) 
     { 
      Byte[] bSize = new Byte[sizeof(Int32)]; 
      s.Read(bSize, 0, bSize.Length); 

      Byte[] bData = new Byte[BitConverter.ToInt32(bSize, 0)]; 
      s.Read(bData, 0, bData.Length); 

      TheMessage m = ByteArrayObject<TheMessage>(bData); 
     } 
+0

Okey,現在可以使用。除了'Receiving()'你寫了's.Read()'和'Read()'是一個'NetworkStream'而不是'Socket'的方法,但是我創建了一個'NetworkStream(s)',所以它是精細。 我傳輸的數據沒有得到錯誤,但我沒有看到文本可視化。我發現無論何時從客戶端發送smth,服務器控制檯窗口上的光標都向下一行,但沒有可視化的文本。這應該是因爲我目前沒有任何數據作爲'String'來寫入控制檯,所以它只是創建新行。所以可能我根本沒有得到任何信息......? – Milkncookiez