简体   繁体   English

如何在没有标头的TCP协议中使用boost :: asio来告知消息的大小?

[英]How can I use boost::asio for a TCP protocol without a header which tells me the message's size?

The Boost chat server example demonstrates handling a simple TCP message protocol in which each message is preceded by a fixed-size header which tells you the size of the message which follows. Boost 聊天服务器示例演示了如何处理简单的TCP消息协议,其中每个消息之前都有一个固定大小的标头,该标头告诉您随后的消息的大小。 This means you always know exactly how many bytes to read in your next call to async_read() ; 这意味着您始终确切知道在下一次对async_read()调用中要读取多少个字节; you alternate between reading a header whose size is always the same, and a message whose size is given in the header. 您可以在读取大小始终相同的标头和在标头中指定大小的消息之间交替。 This works well with the Boost i/o service model, which promises to call a handler when exactly the expected number of bytes have been received from the socket. 这与Boost I / O服务模型配合得很好,Boost I / O服务模型承诺在从套接字接收到确切的预期字节数时调用处理程序。

How can I use Boost to run a TCP protocol which doesn't use a header like this? 如何使用Boost运行不使用标头的TCP协议? My client has a protocol which uses special byte sequences to represent the start and end of each message, so I won't know how many bytes to read in each call to async_read() ; 我的客户有一个协议,该协议使用特殊的字节序列表示每个消息的开始和结束,因此我不知道每次对async_read()调用中要读取多少个字节; I have to just get bytes from the socket as they arrive and watch for the special byte sequences. 我必须在套接字到达时从套接字获取字节,并注意特殊的字节序列。 If I pick a sensible buffer size like 256 bytes, and if my handler will only be called when that many bytes have been read, I believe the i/o service will generally end up receiving the last few bytes of the most recent message from the network, but not passing them to my handler until the next message comes along and brings the byte total up to the number I'm expecting. 如果我选择了一个合理的缓冲区大小(例如256个字节),并且只有在读取到这么多字节后才调用我的处理程序,我相信I / O服务通常最终会从Windows接收最新消息的最后几个字节。网络,但是直到下一条消息出现之前,它们都不会传递给我的处理程序,并且使字节总数达到我期望的数量。 The next message may not arrive for some time, and I want to handle the current message as soon as it arrives. 下一条消息可能会在一段时间内没有到达,我想在当前消息到达后立即处理。

Reading one byte at a time isn't a good idea for performance reasons, correct? 出于性能原因,一次读取一个字节不是一个好主意,对吗?

http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/examples.html http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/examples.html

There is few options: 有几种选择:

  • You can use async_read_until to read until your "ending sequence"(so until end of message). 您可以使用async_read_until进行读取直到您的“结束序列”(因此直到消息结束)。

  • If your "ending sequence" depends on "starting sequence", you can make it to read fixed buffer (equal to starting sequence length); 如果“结束序列”取决于“起始序列”,则可以使其读取固定缓冲区(等于起始序列长度); calculate the ending sequence; 计算结束顺序; and then setup async_read_until . 然后设置async_read_until

  • Also, you can make call to async_read_some to read any amount of bytes which arrived into socket buffer. 另外,您可以调用async_read_some以读取到达套接字缓冲区的任何字节数。 Then check your buffer with your own function for containing complete packet or need to read next part. 然后使用自己的功能检查缓冲区是否包含完整的数据包,或者需要阅读下一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++:boost :: asio:可用于TCP套接字的最大缓冲区大小是多少? - C++: boost::asio : what's the maximum buffer size I can use for TCP sockets? 如何使用 BOOST ASIO 实现重新发送消息的协议 - How to implement protocol which resends messages using BOOST ASIO 我应该如何使用初始化 boost::asio::ip::tcp::socket? 不使用构造函数(我需要默认构造)包括(boost::asio::ip::tcp::socket) - how shoud i use initialize boost::asio::ip::tcp::socket? not using constructor (i need default construct) including(boost::asio::ip::tcp::socket) Boost ASIO - TCP 数据包的报头和正文无效 - Boost ASIO - Invalid Header and Body for TCP packet Boost ASIO - 无法通过 TCP 发送消息 - Boost ASIO - Unable to send message over TCP 我可以对HTTPS请求使用boost asio吗 - Can I use boost asio for HTTPS requests 如何使用boost :: asio中的“chunked”HTTP Transfer-Protocol将文件写入套接字? - How can I write a file to a socket using the 'chunked' HTTP Transfer-Protocol in boost::asio? 如何通过Boost绑定使用Boost :: asio :: buffer(buf,size)? - How to use Boost::asio::buffer(buf, size) with boost bind? 如何使用boost :: asio :: ip :: tcp :: iostream发送POST? - How do I send a POST with boost::asio::ip::tcp::iostream? 使用Boost ASIO在没有套接字的情况下处理TCP - Using Boost ASIO to process TCP without sockets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM