Coap.Net 自定义设置

Coap.Net Custom Settings

我是 C# 的初学者,但我正在编写一个 CoAP 客户端,它将同时测试多个 CoAP 服务器的吞吐量和响应时间。仅供参考,该应用程序是作为命令行程序实现的。 我使用的库是 CoAP.NET.

一切都很顺利,直到我想将默认块大小从 512 字节更改为小于 32 字节的大小。

这是我对 C# 的理解失败的地方,设置位于接口 ICoapConfig 和 class CoapConfig。这些设置在某些时候会神奇地加载。

我试过这样做:

CoapConfig conf = new CoapConfig();
conf.DefaultBlockSize = 32;
CoapClient client = new CoapClient(conf);
...
Request req = new Request(Method.PUT);
req.SetPayload(payload, MediaType.TextPlain);
client.Send(req);

然而,块大小仍然是 512,因为消息仅在超过 512b 时才按块传输。

DEBUG - BlockwiseLayer uses MaxMessageSize: 1024 and DefaultBlockSize:512

这是我在 运行 程序时得到的第一行输出。

很高兴得到任何帮助 :)

解决方案:

CoapConfig conf = new CoapConfig();
conf.DefaultBlockSize = 32;
...
Request req = new Request(Method.PUT);
req.SetPayload(payload, MediaType.TextPlain);
req.URI = client.Uri;

IEndPoint _clientEndpoint;
_clientEndpoint = new CoAPEndPoint(conf);
_clientEndpoint.Start();
req.Send(_clientEndpoint);
...

我真的不知道这个 EndPoint 可能是什么,但这可以让您在发送时使用所需的配置。 仅供参考:我尝试将服务器的配置设置为但没有成功。

您可以在 github 上查看 CoAP.NET 的所有源代码。

我不熟悉这个库,但从快速回顾来看,DefaultBlockSize 配置 属性 似乎只被 CoapServer 引用(而不是 CoapClient).

进一步的证据在 BlockTransferTest fixture, which configures the CoapServer with a particular block size on line 43

您还可以查看同一测试 line 161 class。看来您也可以为端点提供配置设置。