通讯协议 [英] Messaging Protocol

查看:98
本文介绍了通讯协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何设计消息协议,以便区分用户数据和消息终止.

How should I design my message protocol so that I distinguish between user data and termination of the message.

例如,我从用户那里获取输入,以通过TCP/IP将其发送给客户端,现在客户端需要知道消息的长度,我认为我使用某种终止来使客户端识别该消息已经达到终点了?

For example I take input from user to send it to client over TCP/IP, now the client-side needs to know the length of message, what I thought that I use some kind of termination by which the client recognizes that message end has been reached?

在C ++中,我该如何处理?我正在使用Windows套接字

How do I something like this in C++. I'm using Windows sockets

推荐答案

对于特定于应用程序的协议,一种合理的通用策略是在消息之前编码消息长度,而不是使用终止.因此,前几个字节(有多少个字节)指定了消息的长度,而某些特殊代码(通常所有位都打开或所有位都关闭)指定了需要继续的超长消息.

A reasonably common strategy for application-specific protocols is, instead of using termination, to encode message length before the message. So the first few bytes (how many varies) specify the message length, and some special code (usually all bits on or all bits off) designates an overlong message that requires continuation.

编辑:已请求一个示例.假设我们已经设计了协议,以使消息的最大大小为255个字节,并且我们想发送字符串"hi".我们的协议消息将由三个字节组成:2104105.第一个字节2告诉我们紧随其后的消息的长度.第二个和第三个字节104105hi的ASCII.

An example has been requested. Let's say we've designed our protocol so that the maximum size of a message is 255 bytes, and we want to send the string "hi". Our protocol message will consist of three bytes: 2, 104, 105. The first byte, 2, tells us the length of the message that follows it. The second and third bytes, 104 and 105, are ASCII for h and i.

这篇关于通讯协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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