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);
}
原文地址:http://fengyuzaitu.blog.51cto.com/5218690/1971202