码迷,mamicode.com
首页 > 其他好文 > 详细

boost read_some函数疑惑

时间:2017-10-11 00:42:34      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:boost   asio   read   

read_some一旦有遇到数据发送过来,就会立刻返回,但是怎么知道数据是否已经发送结束

目前的情况下,是继续等待接收,直到遇到协商的结束符号.如果read_some返回数据是0,代表对端已经关闭了

void ReadSomeFunc()

{

boost::system::error_code ec;

do

{

char szRecvBuf[10240] = { 0 };

int nReadLen = m_socket.read_some(boost::asio::buffer(szRecvBuf), ec);

if (0 == nReadLen)

{

char szRecvBufLeft[10240] = { 0 };

nReadLen = m_socket.read_some(boost::asio::buffer(szRecvBufLeft), ec);

if (0 == nReadLen) return;


m_strMatch = m_strMatch + szRecvBufLeft;

}

if (ec) return;


m_strMatch = szRecvBuf;

int nIndexOfContentLength = m_strMatch.find("Content-Length:", 0);

int indexOfEnd = m_strMatch.find("\r\n\r\n", 0);

if (nIndexOfContentLength == -1) break;


if (-1 == indexOfEnd) break;


std::string strContextLen = m_strMatch.substr(nIndexOfContentLength + 15, indexOfEnd - nIndexOfContentLength - 15);

unsigned int nContextLen = atoi(strContextLen.c_str());

if (nContextLen > m_strMatch.length())

{

char szRecvBufLeft[40960] = { 0 };

m_socket.read_some(boost::asio::buffer(szRecvBufLeft), ec);

m_strMatch = m_strMatch + szRecvBufLeft;

}


boost::asio::streambuf request;

std::ostream request_stream(&request);

request_stream << "HTTP/1.1 200 OK\r\n";

request_stream << "\r\n";

boost::asio::write(m_socket, request, ec);

} while (0);

}


boost read_some函数疑惑

标签:boost   asio   read   

原文地址:http://fengyuzaitu.blog.51cto.com/5218690/1971202

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!