Byte[] buff = new Byte[2048];
sock.Receive(buff);//接收返回
怎么把这2个结构体弄出来
public struct statisticalData
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public char[] serviceUpTime; //服务启动时间
public long connectionsSoFar; //累计连接数量
public int currentConnections; //当前连接数量
public int concurrenceMax; //最大并发连接数量
public long totalRead; //累计收取数据量
public long totalWrite; //累计发送数据量
public long totalReadMessage; //累计接收消息包数量
public long totalWriteMessage; //累计发送消息包数量
public int maxReadSpeed; //最高接收速率
public int maxWriteSpeed; //最高发送速率
public int currentReadSpeed; //当前接收速率
public int currentWriteSpeed; //当前发送速率
} ; public struct stClientHead
{
public Int32 nLen; //后续数据长度
public byte nType; //消息类型
public Int32 nPVersion; //协议版本
public Int32 nSocketFd; //socketfd
public byte bPriority; //连接的优先级(消息类型等于106启用)
}
sock.Receive(buff);//接收返回
怎么把这2个结构体弄出来
public struct statisticalData
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public char[] serviceUpTime; //服务启动时间
public long connectionsSoFar; //累计连接数量
public int currentConnections; //当前连接数量
public int concurrenceMax; //最大并发连接数量
public long totalRead; //累计收取数据量
public long totalWrite; //累计发送数据量
public long totalReadMessage; //累计接收消息包数量
public long totalWriteMessage; //累计发送消息包数量
public int maxReadSpeed; //最高接收速率
public int maxWriteSpeed; //最高发送速率
public int currentReadSpeed; //当前接收速率
public int currentWriteSpeed; //当前发送速率
} ; public struct stClientHead
{
public Int32 nLen; //后续数据长度
public byte nType; //消息类型
public Int32 nPVersion; //协议版本
public Int32 nSocketFd; //socketfd
public byte bPriority; //连接的优先级(消息类型等于106启用)
}
IntPtr structPtr = Marshal.AllocHGlobal(nSize);
Marshal.Copy(bufferTemp, 0, structPtr, nSize);
object obj = Marshal.PtrToStructure(structPtr, typeof(statisticalData1));
Marshal.FreeHGlobal(structPtr);
statisticalData strtemp = (statisticalData )obj;
IntPtr _ClientHeadIntPtr = Marshal.AllocHGlobal(_ClientHeadSize);
Marshal.Copy(_Value, 0, _IntPtr, _ClientHeadSize);
object _ClientHeadOBject = Marshal.PtrToStructure(_ClientHeadIntPtr, typeof(stClientHead));
int _DataSize = Marshal.SizeOf(typeof(statisticalData));
IntPtr _DataIntPtr = Marshal.AllocHGlobal(_DataSize);
Marshal.Copy(_Value, _ClientHeadSize, _DataIntPtr, _DataSize);
object _ClientHeadOBject = Marshal.PtrToStructure(_DataIntPtr, typeof(stClientHead));
看来你是需要把代码喂到嘴边才行。
Byte[] buff = new Byte[2048];
sock.Receive(buff);//接收返回 int _ClientHeadSize = Marshal.SizeOf(typeof(stClientHead));
IntPtr _ClientHeadIntPtr = Marshal.AllocHGlobal(_ClientHeadSize);
Marshal.Copy(buff, 0, _ClientHeadIntPtr , _ClientHeadSize);
stClientHead _ClientHeadOBject = (stClientHead)Marshal.PtrToStructure(_ClientHeadIntPtr, typeof(stClientHead));
Marshal.FreeHGlobal(_ClientHeadIntPtr); int _DataSize = Marshal.SizeOf(typeof(statisticalData));
IntPtr _DataIntPtr = Marshal.AllocHGlobal(_DataSize);
Marshal.Copy(buff, _ClientHeadSize, _DataIntPtr, _DataSize);
statisticalData _ClientHeadOBject = (statisticalData)Marshal.PtrToStructure(_DataIntPtr, typeof(statisticalData));
Marshal.FreeHGlobal(_DataIntPtr);
Marshal.Copy方法的参数里有指明在buff中的偏移量,你自己看第二个参数,第一次用的时候是0,第二次是_ClientHeadSize,也就是从上次读取的最后开始。