boost::asio::streambuf基本用法和注意事项

streamsize  sgetn(char_type *store,streamsize n)    返回缓冲区下n个字符并存储到store中,并将缓冲区位置后移n个字节

成都创新互联是专业的湖南网站建设公司,湖南接单;提供成都网站建设、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行湖南网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

代码说明:本来是想不断的通过sgetn函数获取到streambuf的内容,由于没有完全理解sgetn获取流的方式,导致了问题的产生

int Teststreambuf()

{

boost::asio::streambuf request;

std::ostream request_stream(&request);

request_stream << "GET /cs/restfull/operationRestfullApi/testGet/ HTTP/1.1\r\n";

request_stream << "Accept: application/x-ms-application, p_w_picpath/jpeg, application/xaml+xml, p_w_picpath/gif, p_w_picpath/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";

request_stream << "Accept-Language: zh-CN\r\n";

request_stream << "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)\r\n";

request_stream << "Host: " << "192.168.0.88:8080" << "\r\n";

request_stream << "Connection: Keep-Alive\r\n\r\n";

//当第一次使用sgetn函数的时候,已经将所有的数据读完,指向流的结束位置

char buf[1024] = { 0 };

int len = request.size();

request.sgetn(buf, len);

//当第二次使用sgetn函数的时候,由于第一次函数调用指向了流的结束位置,

//所以第二次读取到的buf1是空的

char buf1[1024] = { 0 };

int len1 = request.size();

request.sgetn(buf1, len1);

return 0;

}

实际的测试环境:希望组装完成一次HTTP请求之后,循环发送构建的数据包,达到压力测试的结果,但是在接收端只是接收到一次的数据,初步怀疑是服务器的代码有问题,后面才发现是sgetn函数的使用方法有问题

#include

#include

#include

#include

using namespace std;

using namespace boost::asio;

int Teststreambuf()

{

io_service iosev;

ip::tcp::socket socket(iosev);

ip::tcp::endpoint ep(ip::address_v4::from_string("127.0.0.1"), 7002);

boost::system::error_code ec;

socket.connect(ep, ec);

if (ec) return -1;

boost::asio::streambuf request;

std::ostream request_stream(&request);

request_stream << "POST /cs/restfull/operationRestfullApi/testPost HTTP/1.1\r\n";

request_stream << "Accept: application/x-ms-application, p_w_picpath/jpeg, application/xaml+xml, p_w_picpath/gif, p_w_picpath/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";

request_stream << "Accept-Language: zh-CN\r\n";

request_stream << "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)\r\n";

request_stream << "Host: " << "192.168.0.88:8080" << "\r\n";

request_stream << "Connection: Keep-Alive\r\n\r\n";

while (1)

{

//第一次调用write之后,request的数据已经发送完毕,作为一个流,已经指向数据的末尾,所以

//当再次循环的时候发送的数据len==0,本意是循环发送上面的HTTP数据,对服务器进行压力测试

size_t len = boost::asio::write(socket, request);

std::cout << len << std::endl;

::Sleep(300);

}

return 0;

}

修改之后的代码

#include

#include

#include

#include

using namespace std;

using namespace boost::asio;

int Teststreambuf()

{

io_service iosev;

ip::tcp::socket socket(iosev);

ip::tcp::endpoint ep(ip::address_v4::from_string("127.0.0.1"), 7002);

boost::system::error_code ec;

socket.connect(ep, ec);

if (ec) return -1;

boost::asio::streambuf request;

std::ostream request_stream(&request);

request_stream << "POST /cs/restfull/operationRestfullApi/testPost HTTP/1.1\r\n";

request_stream << "Accept: application/x-ms-application, p_w_picpath/jpeg, application/xaml+xml, p_w_picpath/gif, p_w_picpath/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";

request_stream << "Accept-Language: zh-CN\r\n";

request_stream << "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)\r\n";

request_stream << "Host: " << "192.168.0.88:8080" << "\r\n";

request_stream << "Connection: Keep-Alive\r\n\r\n";

//跟上面的不一样的做法,是将request的数据一次性读取到buf数组中,然后循环发送buf的数据内容

 char buf[1024] = { 0 };

 request.sgetn(buf, request.size());

while (1)

{

size_t len = boost::asio::write(socket, boost::asio::buffer(buf));

std::cout << len << std::endl;

::Sleep(300);

}

return 0;

}


本文题目:boost::asio::streambuf基本用法和注意事项
地址分享:http://myzitong.com/article/jhgoig.html