C ++ Boost async_read_some +缓冲区中的旧数据

时间:2018-12-31 00:57:13

标签: c++ boost boost-asio http-proxy

有一个小型代理服务器可以中继请求-接收答案-提供浏览器。 +/-一切正常,但是有一个错误。 我通过以下方式从客户端接收数据:

   void handle_handshake(const boost::system::error_code& error, int num)
    {
    if (!error)
    {

        socket_.async_read_some(boost::asio::buffer(data_, 1024),
            boost::bind(&Session::handle_read, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred, num));
    }
    else
    {
        delete this;
    }
}

void handle_read(const boost::system::error_code& error,
    size_t bytes_transferred, int num)
{
    if (!error)
    {

std::cout << "data_: " << data_ << std::endl;

}

}

在data_(char data_ [1024])中写入数据,有时它从旧请求中获取数据,例如:

HTTP/1.0 200 OK
Content-Type: application/json
UserIP: 127.0.0.1
Host: test.com
Connection: keep-alive
Origin: https://test.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/70.0.3538.110 Safari/537.36
Content-Type: application/json
Accept: */*
Referer: https://test.com/
Accept-Language: ru,en-US;q=0.9,en;q=0.8,uk;q=0.7
Cookie: _ga=GA1.2.508780938.1527764085
Accept-Encoding: gzip, deflate
Content-Length: 39

{"code":"-1","msg":"","result":"err"}

Accept-Language: ru,en-US;q=0.9,en;q=0.8,uk;q=0.7
Cookie: _ga=GA1.2.508780938.1527764085
Accept-Encoding: gzip, deflate
Content-Length: 104

相应地,该软件包应以{“ code”:“-1”,“ msg”:“”,“ result”:“ err”}结尾...

请帮忙。读取后如何清除缓冲区或如何组织从套接字读取数据?预先谢谢你!

0 个答案:

没有答案