2015-02-08 102 views
0

嗯,我想轉換字符串的大字節的信息。 (長度爲11076)UTF8字節到字符串和Winsock GetStream

問題到底是什麼,信息中缺少字符。 (長度10996)

看:

enter image description here

的信息由Winsock連接收到,看proccess:

public static void UpdateClient(UserConnection client) 
    { 
     string data = null; 
     Decoder utf8Decoder = Encoding.UTF8.GetDecoder(); 

      Console.WriteLine("Iniciando"); 
      byte[] buffer = ReadFully(client.TCPClient.GetStream(), 0); 
      int charCount = utf8Decoder.GetCharCount(buffer, 0, buffer.Length); 
      Char[] chars = new Char[charCount]; 
      int charsDecodedCount = utf8Decoder.GetChars(buffer, 0, buffer.Length, chars, 0); 

      foreach (Char c in chars) 
      { 
       data = data + String.Format("{0}", c); 
      } 

      int buffersize = buffer.Length; 
      Console.WriteLine("Chars is: " + chars.Length); 
      Console.WriteLine("Data is: " + data); 
      Console.WriteLine("Byte is: " + buffer.Length); 
      Console.WriteLine("Size is: " + data.Length); 
      Server.Network.ReceiveData.SelectPacket(client.Index, data); 
    } 

    public static byte[] ReadFully(Stream stream, int initialLength) 
    { 
     if (initialLength < 1) 
     { 
      initialLength = 32768; 
     } 

     byte[] buffer = new byte[initialLength]; 
     int read = 0; 

     int chunk; 

     chunk = stream.Read(buffer, read, buffer.Length - read); 

     checkreach: 
      read += chunk; 

      if (read == buffer.Length) 
      { 
       int nextByte = stream.ReadByte(); 

       if (nextByte == -1) 
       { 
        return buffer; 
       } 

       byte[] newBuffer = new byte[buffer.Length * 2]; 
       Array.Copy(buffer, newBuffer, buffer.Length); 
       newBuffer[read] = (byte)nextByte; 
       buffer = newBuffer; 
       read++; 
       goto checkreach; 
      } 

     byte[] ret = new byte[read]; 
     Array.Copy(buffer, ret, read); 
     return ret; 
    } 

任何人有提示或解決方案?

+1

Ewww'goto'。你應該重寫你的緩衝區複製代碼。 – Dai 2015-02-08 23:19:15

+0

是的,錯誤,但這不是問題。 – user3571412 2015-02-08 23:24:18

+0

您發送的字符數是否正確?看起來您需要將字符串編碼爲UTF字節,然後發送字節數組長度而不是UTF字節的字符串長度。 – 2015-02-08 23:33:32

回答

0

UTF-8編碼文本的字節數比字符數多,這是完全正常的。在UTF-8中,某些字符(例如áã)被編碼爲兩個或更多字節。

由於ReadFully方法返回垃圾如果您嘗試使用它來讀取超過適合初始緩衝區,或者如果它不能讀取整個流與一個Read調用,您不應該使用它。字符數組轉換爲字符串的方式也非常緩慢。只需使用StreamReader來讀取流並將其解碼爲字符串:

public static void UpdateClient(UserConnection client) { 
    string data; 
    using (StreamReader reader = new StreamReader(client.TCPClient.GetStream(), Encoding.UTF8)) { 
    data = reader.ReadToEnd(); 
    } 
    Console.WriteLine("Data is: " + data); 
    Console.WriteLine("Size is: " + data.Length); 
    Server.Network.ReceiveData.SelectPacket(client.Index, data); 
} 
+0

我收到錯誤>流式閱讀器「不包含」數據「的定義」 – user3571412 2015-02-08 23:44:10

+0

@ user3571412:我在發佈答案後立即對代碼進行了一些小的更正。更新頁面,以便看到當前的代碼。 – Guffa 2015-02-08 23:47:27

+0

問題仍然存在,現在用一個新的「語法錯誤」,「」 – user3571412 2015-02-08 23:50:52