基于结构的C#udp包 [英] C# udp packet based on a structure

查看:152
本文介绍了基于结构的C#udp包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



我想通过udp端口向打印机发送数据。我有一个udp数据包的结构,但不知道如何构建它。



结构



有没有人有想法?



我是什么尝试过:



Hello

I want to send a data to the printer via udp port. I have a structure of that udp packet but have no idea how to build it.

The structure

Does anyone have an idea?

What I have tried:

UdpClient udpClient = new UdpClient();
            udpClient.Connect("192.168.0.99", 47545);
            Byte[] senddata = Encoding.ASCII.GetBytes("id_val_operation_acquire_records2");
            udpClient.Send(senddata, senddata.Length);

推荐答案

创建对应于结构定义的C#结构。此结构不得包含填充字节( Pack = 1 )并使用固定宽度的成员(使用 byte UInt16 UInt32 根据字段的长度而定)。请参阅 StructLayoutAttribute.Pack Field(System.Runtime .InteropServices) [ ^ ]。



然后创建一个这个结构的实例,填充它并发送它。发送需要将结构转换为字节数组,这是一项常见任务;只需搜索c#structure to byte array。
Create a C# structure that corresponds to the structure definition. This structure must not contain padding bytes (Pack = 1) and use members with fixed width (use byte, UInt16, UInt32 according to the length of the fields). See StructLayoutAttribute.Pack Field (System.Runtime.InteropServices)[^].

Then create an instance of this structure, fill it, and send it. Sending requires converting the structure to a byte array which is a common task; just search for "c# structure to byte array".


这篇关于基于结构的C#udp包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆