// 结构体序列化 public static byte[] struct2Bytes(CAR_TO_TRUCK_MESSAGE structObj) { int size = Marshal.SizeOf(structObj); byte[] bytes = new byte[size]; IntPtr structPtr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(structObj, structPtr, false); Marshal.Copy(structPtr, bytes, 0, size); Marshal.FreeHGlobal(structPtr); return(bytes); }
///发送数据 public void dataSend(CAR_TO_TRUCK_MESSAGE message) { // 实名发送 // 填写数据 byte[] sendbytes = struct2Bytes(message); IPEndPoint remoteIpep = new IPEndPoint( IPAddress.Parse(remote_ip), 41001); // 发送到的IP地址和端口号 udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep); }