如何使用Boost.Asio读取WHOLE char类型捕获的数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Boost.Asio读取WHOLE char类型捕获的数据相关的知识,希望对你有一定的参考价值。

在使用boost :: asio时,我遇到了一个有趣的时刻。问题是我想进行语音聊天并使用OpenAL,我将捕获的数据保存在A字节中:

ALubyte* data; // data has the captured data now
int data_size; 

所以我发送数据将其转换为char *

boost::asio::streambuf m_buf;
std::ostream out(&m_buf);
out << data_size << ';'; // the simbol ';' is used to separate data from each other  
out.write((char*)data, data_size);
out << 'q'; // the simbol 'q' is used for a client side to understand that 
            //this is the end of the stream 
            // as a client where the 'out' stream is sent to, 
            //uses boost::async_read_until --> ooops

boost::asio::async_write(client_sock, m_buf, [](){});

正如您可能已经猜到的那样,问题在于boost :: asio :: async_read_until

//in a client side

boost::asio::streambuf client_buf;
std::istream in(&client_buf);

boost::asio::async_read_until(m_sock, client_buf, 'q', [](){});//---> oooops,   
        // this boost::asio::async_read_until 
        //function reads until the first 'q' simbol which  'data' might 
        //have a lot of 'q' simbol as the captured data is stored as
        // a char type
        //so the function does not read the whole captured data.

伙计们我知道这是我自己选择这个功能的大错误,但我现在不能改变它,因为代码有点大,所以你有一些想法在这种情况下做什么,以便我可以阅读整个捕获数据,或者你认为没有改变boost :: asio :: async_read_until函数就没有出路?任何想法都非常感谢!!!

答案

而不是simbol'q',更好的选择是添加更多的符号如“cpp”,在这种情况下,将捕获的数据保存在char类型中的数据可能没有相同的签名,'q'符号可能很多在数据中,但使用“cpp”...等风险将少于一个'q'辛博尔,这意味着添加的符号越多风险越小!

以上是关于如何使用Boost.Asio读取WHOLE char类型捕获的数据的主要内容,如果未能解决你的问题,请参考以下文章

C++ Boost ASIO:如何读取/写入超时?

如何确定是不是有数据可从 boost::asio 中的套接字读取?

boost::asio 从 /dev/input/event0 读取

Boost asio - 从标准输入异步读取已建立的字符数

为啥 boost::asio::read 缓冲区数据大小小于读取大小?

在使用Boost :: asio之前从套接字读取之后是否可以执行async_handshake?