嗯,我想轉換字符串的大字節的信息。 (長度爲11076)UTF8字節到字符串和Winsock GetStream
問題到底是什麼,信息中缺少字符。 (長度10996)
看:
的信息由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; }
任何人有提示或解決方案?
Ewww'goto'。你應該重寫你的緩衝區複製代碼。 – Dai 2015-02-08 23:19:15
是的,錯誤,但這不是問題。 – user3571412 2015-02-08 23:24:18
您發送的字符數是否正確?看起來您需要將字符串編碼爲UTF字節,然後發送字節數組長度而不是UTF字節的字符串長度。 – 2015-02-08 23:33:32