private bool GetDataLength(IntPtr connId, ref int reallength) { IntPtr bufferPtr = Marshal.AllocHGlobal(7); try { //Peek从数据包中窥视数据,不会影响缓存数据的大小 if (_server.Peek(connId, bufferPtr, 7) == FetchResult.Ok) { byte[] bufferBytes = new byte[7]; Marshal.Copy(bufferPtr, bufferBytes, 0, 7); byte[] len = new byte[4]; Array.Copy(bufferBytes, 2, len, 0, 4); int lens = BitConverter.ToInt32(len, 0); reallength = lens; return(true); } else { return(false); } } catch (Exception ex) { return(false); } finally { Marshal.FreeHGlobal(bufferPtr);//释放申请的内存空间 } }