2010-11-04 20 views
2

有例如HTTP客戶端在http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/example/http/client/async_client.cpp 請幫我變化最大緩衝區大小像下面的代碼解釋(它是從庫中下載的例子,而不是從網站):Boost.Asio的 - 設置最大讀取流大小

void handle_write_request(const boost::system::error_code& err) 
{ 
    if (!err) 
    { 
    // Read the response status line. The response_ streambuf will 
    // automatically grow to accommodate the entire line. The growth may be 
    // limited by <b>passing a maximum size to the streambuf constructor</b>. 
    boost::asio::async_read_until(socket_, response_, "\r\n", 
     boost::bind(&client::handle_read_status_line, this, 
      boost::asio::placeholders::error)); 
    } 
    else 
    { 
    std::cout << "Error: " << err.message() << "\n"; 
    } 
} 

而這裏的響應緩衝區的構造函數:

boost::asio::streambuf response_; 

但是編譯器說,下面的代碼是無效的:

boost::asio::streambuf response_(1024);

似乎默認緩衝區是512字節大小,我需要更大的尺寸。

回答

0

1)我不知道您的512字節的限制是從哪裏來的,因爲構造爲asio::basic_streambuf具有以下特徵(這使得它可以存儲方式超過512個或1024個字節):

explicit basic_streambuf(
    std::size_t max_size = (std::numeric_limits<std::size_t>::max)(), 
    const Allocator& allocator = Allocator()) 

2)此代碼boost::asio::streambuf response_(1024);是無效的,因爲您不能在聲明點初始化成員變量,您必須在構造函數的初始化程序列表或它的主體中執行它。如果你不這樣做,它將被默認初始化。

3)在代碼引薦限制/限制streambuf的最大尺寸的評論 - 所以它肯定不會幫助你得到「尺寸較大」,恰恰相反。

+0

忘記我的512字節限制,我錯了。 – soulmare 2010-11-04 08:07:48

+0

似乎response_變量根本沒有初始化,只是聲明和使用? – soulmare 2010-11-04 08:11:03

+0

@soulmare:'response_'它的默認初始化 – 2010-11-04 08:28:52