如何“OK"消息长什么样? [英] How the "OK" message looks like?

查看:38
本文介绍了如何“OK"消息长什么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ]

是否有标准的OK"消息要返回?和错误"消息?它看起来如何?(例如:0011"、:110F")

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

推荐答案

您必须设计一个应用程序级协议.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...

  • 长度分隔的块.每条消息都以 16 位或 32 位长度的前缀开头.然后是许多字节.长度需要采用定义的字节顺序(参见 htons、ntohs 等).使用此协议的每个人都知道读取长度前缀然后读取那么多字节.在网络上定义了块"之后,您可以在块的内容上放置一个标题.可能是消息类型(ACK、NAK、数据等)后跟一些内容.
  • ASCII 换行符分隔.每条消息都是一行 ASCII(或 UTF8 等)文本.它以换行符结束.行的换行符结尾与上述块的长度前缀起着相同的作用.然后定义每行中的内容(如空格或逗号分隔的 ASCII/UTF8/任何字段).您可以在其中定义数据的外观、ACK 等.
  • 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.

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

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.

这篇关于如何“OK"消息长什么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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