流的c ++长度前缀消息帧 [英] c++ Length-prefix message framing for streams

查看:96
本文介绍了流的c ++长度前缀消息帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试实现客户端/服务器协议缓冲区流.
使用tcp/ip时,问题是要知道一条消息的结尾和一条新消息的开始.
解决方案是实现长度前缀消息传递系统.
有没有一种方法可以同时通过套接字发送message-length + message?
任何代码段都将不胜感激^^

Hi
I''m trying to implement a client/server protocol buffer stream.
When using tcp/ip the issue is to know where a message end and a new message begin.
The solution is to implement length-prefix messaging system.
Is there a way to send message-length + message at the same time through a socket??
Any code snippets would be appreciated ^^
thx in advance.

推荐答案

只需将消息长度作为整数值添加到消息的开头(不要忘记通过 htonl() [
Just add the message length to the beginning of your message as an integer value (don''t forget to convert it via htonl()[^]. You could also send this value independently of your actual text and let the receiver re-assemble it when handling the input messages.


这篇关于流的c ++长度前缀消息帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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