Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的自定义UDP数据包结构#_C#_Sockets - Fatal编程技术网

C# C语言中的自定义UDP数据包结构#

C# C语言中的自定义UDP数据包结构#,c#,sockets,C#,Sockets,我想发送一个UDP数据包,它有以下结构 数据包大小为8字节 uint16数据类型的标题字段 uint16数据类型的命令字段 uint32数据类型的数字字段 我按照以下方式设计了数据包: ushort header = (ushort)IPAddress.HostToNetworkOrder(0x0001); ushort command = (ushort)IPAddress.HostToNetworkOrder(10); uint number = (uint)IPAddress.HostTo

我想发送一个UDP数据包,它有以下结构

  • 数据包大小为8字节
  • uint16数据类型的标题字段
  • uint16数据类型的命令字段
  • uint32数据类型的数字字段
  • 我按照以下方式设计了数据包:

    ushort header = (ushort)IPAddress.HostToNetworkOrder(0x0001);
    ushort command = (ushort)IPAddress.HostToNetworkOrder(10);
    uint number = (uint)IPAddress.HostToNetworkOrder(2);
    
    byte[] requestPacket = new byte[8];
    requestPacket[0] = (byte)(header >> 8 & 0xFF);
    requestPacket[1] = (byte)(header & 0xFF);
    requestPacket[2] = (byte)(command >> 8 & 0xFF);
    requestPacket[3] = (byte)(command & 0xFF);
    requestPacket[4] = (byte)(number >> 24 & 0xFF);
    requestPacket[5] = (byte)(number >> 16 & 0xFF);
    requestPacket[6] = (byte)(number >> 8 & 0xFF);
    requestPacket[7] = (byte)(number & 0xFF);
    

    是否存在更好的方法来执行上述操作?

    您可以使用
    位转换器
    ,例如:

    var headerBytes = BitConverter.GetBytes(header);
    Array.Copy(headerBytes, 0, requestPacket, 0, 2);
    

    您可以使用
    位转换器
    ,例如:

    var headerBytes = BitConverter.GetBytes(header);
    Array.Copy(headerBytes, 0, requestPacket, 0, 2);
    

    您还可以使用.NET的内置封送处理功能或不安全代码:

    [StructLayout(LayoutKind.Sequential, Pack = 1)] // Ensure tight packing
    public struct Packet
    {
        public ushort Header;
        public ushort Command;
        public uint Number;
    }
    
    Packet packet = new Packet { ... };
    byte[] packetData = new byte[Marshal.Sizeof<Packet>()];
    
    // Using marshaling
    var handle = default(GCHandle);
    try
    {
        handle = GCHandle.Alloc(packetData, GCHandleType.Pinned);
        Marshal.StructureToPtr(packet, handle.AddrOfPinnedObject(), fDeleteOld: false);
    }
    finally
    {
        if (handle.IsAllocated) handle.Free();
    }
    
    // Using unsafe code (careful, assumes marshaled size == size in memory!)
    unsafe { fixed(byte* packedPtr = packetData) *(Packet*)packedPtr = packet; }
    
    [StructLayout(LayoutKind.Sequential,Pack=1)]//确保紧密包装
    公共结构包
    {
    公共ushort头;
    公共卫生部;
    公共单位号码;
    }
    数据包=新数据包{…};
    byte[]packetData=新字节[Marshal.Sizeof()];
    //使用编组
    变量句柄=默认值(GCHandle);
    尝试
    {
    handle=GCHandle.Alloc(packetData,GCHandleType.pinted);
    StructureToPtr(数据包,handle.addrofPindedObject(),fDeleteOld:false);
    }
    最后
    {
    if(handle.IsAllocated)handle.Free();
    }
    //使用不安全代码(小心,假设封送大小==内存中的大小!)
    不安全{fixed(byte*packedPtr=packetData)*(Packet*)packedPtr=Packet;}
    

    然而,这两种解决方案都将使用本地机器端性对字段进行编码。这可能是您的问题,也可能不是。您还可以使用.NET的内置封送处理功能或不安全代码:

    [StructLayout(LayoutKind.Sequential, Pack = 1)] // Ensure tight packing
    public struct Packet
    {
        public ushort Header;
        public ushort Command;
        public uint Number;
    }
    
    Packet packet = new Packet { ... };
    byte[] packetData = new byte[Marshal.Sizeof<Packet>()];
    
    // Using marshaling
    var handle = default(GCHandle);
    try
    {
        handle = GCHandle.Alloc(packetData, GCHandleType.Pinned);
        Marshal.StructureToPtr(packet, handle.AddrOfPinnedObject(), fDeleteOld: false);
    }
    finally
    {
        if (handle.IsAllocated) handle.Free();
    }
    
    // Using unsafe code (careful, assumes marshaled size == size in memory!)
    unsafe { fixed(byte* packedPtr = packetData) *(Packet*)packedPtr = packet; }
    
    [StructLayout(LayoutKind.Sequential,Pack=1)]//确保紧密包装
    公共结构包
    {
    公共ushort头;
    公共卫生部;
    公共单位号码;
    }
    数据包=新数据包{…};
    byte[]packetData=新字节[Marshal.Sizeof()];
    //使用编组
    变量句柄=默认值(GCHandle);
    尝试
    {
    handle=GCHandle.Alloc(packetData,GCHandleType.pinted);
    StructureToPtr(数据包,handle.addrofPindedObject(),fDeleteOld:false);
    }
    最后
    {
    if(handle.IsAllocated)handle.Free();
    }
    //使用不安全代码(小心,假设封送大小==内存中的大小!)
    不安全{fixed(byte*packedPtr=packetData)*(Packet*)packedPtr=Packet;}
    
    然而,这两种解决方案都将使用本地机器端性对字段进行编码。这对你来说可能是个问题,也可能不是