提高阅读ASIO数目不详的字节 [英] Boost asio read an unknown number of bytes

查看:132
本文介绍了提高阅读ASIO数目不详的字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种情况:


  1. 客户端连接,不发送字节,并等待服务器的响应。

  2. 客户端连接,发送超过1个字节,然后等待服务器的响应。

问题是下一:
在第一种情况我应该读任何字节,并得到一些服务器响应。
在第二种情况,我应该至少​​读1字节,才把我会得到一个服务器响应。
如果我尝试至少阅读0字节,是这样的:

Problem is next: in 1st case I should read no bytes and get some server response. in 2nd case I should read at least 1 byte and only then I'll get a server response. If i try to read at least 0 bytes, like this:

async_read(sock, boost::asio::buffer(data),
            boost::asio::transfer_at_least(0),
            boost::bind(&server::read, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));

我将永远不会得到适当的服务器reposonse在第二种情况。

I will never get proper server reposonse in 2nd case.

但是,如果我不是这个async_read操作读至少1个字节永远不会结束。

But if I read at least 1 byte than this async_read operation will never ends.

那么,如何处理这个情况?

So, how can I process this cases?

更新1:
我还在寻找解决方案,而无需使用时间限制。

Update 1: I'm still searching for solution without using time limit.

推荐答案

你怎么能指望这个工作?是否响应中的第一和第二壳体之间改变?如果它确实有所不同,你不能因为有一个竞争条件,你应该解决协议做到这一点可靠。如果没有变化,服务器应该只发送响应。

How do you expect this to work? Does the response vary between the first and second case? If it does vary, you cannot do this reliably because there is a race condition and you should fix the protocol. If it does not vary, the server should just send the response.

这个问题的解决方案不是一个ASIO问题

The solution to this problem is not an asio issue.

这篇关于提高阅读ASIO数目不详的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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