如何“确定”?消息看起来像?

发布于 2024-10-07 16:57:44 字数 257 浏览 6 评论 0原文

我有一个向服务器发送数据的设备。

            Data
[ Client ]  == >  [ Server ]

在服务器上验证后,我想返回一条消息:

            OK
[ Client ] < == [ Server ]

是否有标准的“OK”消息要返回?还有“错误”消息?看起来怎么样? (例如“:0011”,“:110F”)

I have a device that sends data to a server.

            Data
[ Client ]  == >  [ Server ]

After the validation on the server I want to return a message:

            OK
[ Client ] < == [ Server ]

Is there a standard "OK" message to return? And an "ERROR" message? How does it looks like? (e.g. ":0011", ":110F")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2024-10-14 16:57:44

您必须设计一个应用程序级协议。 TCP 是字节流,因此即使客户端->服务器部分中“数据”的定义也需要某种协议,以便接收方可以知道数据由哪些字节组成(何时停止读取)。

几种常见的协议类型是...

  • 长度分隔的块。每条消息均以 16 或 32 位长度前缀开头。然后是那么多字节。长度需要采用定义的字节顺序(请参阅htons、ntohs 等)。每个使用此协议的人都知道读取长度前缀,然后读取那么多字节。在网络上定义了该“块”后,您可以在该块的内容上放置一个标头。也许是消息类型(ACK、NAK、数据等),后跟一些内容。
  • ASCII 换行符分隔。每条消息都是一行 ASCII(或 UTF8 等)文本。它以换行符结束。行的换行符结尾与上面块的长度前缀起着相同的作用。然后定义每行的内容(例如空格或逗号分隔的 ASCII/UTF8/任何字段)。在其中的某个地方,您可以定义数据的样子、ACK 等。

我相信您可以提出其他想法,但这是基本工作:在 TCP 字节流之上定义应用程序级协议。

You've got to design an application-level protocol. TCP is a byte stream, so even the definition of "Data" in your client->server piece needs some protocol so that the receiver can know what bytes make up the data (when to stop reading).

A couple of common types of protocols are...

  • Length-delimited chunks. Every message starts with a 16 or 32-bit length prefix. Then that many bytes follow. The length needs to be in a defined byte order (see htons, ntohs, etc). Everyone who uses this protocol knows to read the length prefix then read that many bytes. Having defined that "chunk" on the network, you might put a header on the contents of the chunk. Maybe a message type (ACK, NAK, Data, etc) followed by some contents.
  • ASCII newline delimited. Each message is a line of ASCII (or UTF8, etc) text. It ends at a newline. Newline endings for the lines play the same role as the length prefix for the chunks above. You then define what's in each line (like space or comma-delimited ASCII/UTF8/whatever fields). Somewhere in that you'd define what data looks like, ACK, etc.

I'm sure you could come up with other ideas, but that's the basic job: defining your application-level protocol on top of TCP's byte stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文