Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Boost.Asio什么时候调用async\u read\u一些回调?_C++_Sockets_Boost_Tcp - Fatal编程技术网

C++ Boost.Asio什么时候调用async\u read\u一些回调?

C++ Boost.Asio什么时候调用async\u read\u一些回调?,c++,sockets,boost,tcp,C++,Sockets,Boost,Tcp,假设我有一个Boos.Asio TCP客户端,它可以发送字符串,如下所示: client.sendStr("1111"); received string: "1111", size: 5. received string: "111122", size: 7. received string: "22333", size: 6. received string: "3", size: 2. 我还有一个异步Boost.Asio TCP服务器,它将处理传入的数据包并打印它们,如下所示: cl

假设我有一个Boos.Asio TCP客户端,它可以发送字符串,如下所示:

client.sendStr("1111");
received string: "1111", size: 5.
received string: "111122", size: 7.
received string: "22333", size: 6.
received string: "3", size: 2.
我还有一个异步Boost.Asio TCP服务器,它将处理传入的数据包并打印它们,如下所示:

client.sendStr("1111");
received string: "1111", size: 5.
received string: "111122", size: 7.
received string: "22333", size: 6.
received string: "3", size: 2.
问题是,当我多次调用
client.sendStr()
时,就像这样

client.sendStr("1111");
client.sendStr("2222");
client.sendStr("3333");
我可能会在服务器端得到各种结果,例如:

received string: "1111", size: 5.
received string: "2222", size: 5.
received string: "2222", size: 5.

我是一个网络新手,但我想出现这种情况的原因是TCP是一种“基于流的协议”

我的问题是:

1) 我能阻止Boost.Asio这样做吗?(假设“1111”和“2222”是我的应用程序的控制数据包,我不知道它们要连接什么)

2) 我是否有可能收到这样的回电:

client.sendStr("1111");
received string: "1111", size: 5.
received string: "111122", size: 7.
received string: "22333", size: 6.
received string: "3", size: 2.
对于
async\u read\u some

备注

读取操作可能无法读取所有请求的字节数。 如果需要确保该函数,请考虑使用 在异步操作之前读取请求的数据量 完成

TCP是一种基于流的协议,您需要在应用层处理消息帧。这在很大程度上取决于您的协议,在您的场景中,一个可能的解决方案可能是空终止每个消息。或者,在指示有效负载长度的每条消息之前包含一个固定字节头