Sign in to follow this  
Rickert

How async_read_some() in boost::asio::ip::tcp::socket actually works?

Recommended Posts

It is written in the document: "This function is used to asynchronously read data from the stream socket. The function call always returns immediately. "

I know it is asynchronous, so it returns immediately. But what does async_read_some() differ from free functio read()? When I try to std::cout my buffer used for async_read_some(), it seems that the function reads many times until the stream is out of data.

Does this mean async_read_some() request continuously until it receives every data, for example, in a HTTP GET request? And the server will write little at a time and send a little to the client (for async_read_some() to read a little bit of whole data), or it dumps all data to the client at once?

Example from [url="http://en.highscore.de/cpp/boost/index.html"]The Boost C++ Libraries - Chapter 7[/url]

[code]

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <iostream>
#include <string>

boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::socket sock(io_service);
boost::array<char, 4096> buffer;

void read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred)
{
if (!ec)
{
std::cout << std::string(buffer.data(), bytes_transferred) << std::endl;
std::cout << "Byte transfered: " << bytes_transferred << std::endl;
sock.async_read_some(boost::asio::buffer(buffer), read_handler);
}
}

void connect_handler(const boost::system::error_code &ec)
{
if (!ec)
{
boost::asio::write(sock, boost::asio::buffer("GET / HTTP 1.1\r\nHost: google.com\r\n\r\n"));
sock.async_read_some(boost::asio::buffer(buffer), read_handler);
}
}

void resolve_handler(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it)
{
if (!ec)
{
sock.async_connect(*it, connect_handler);
}
}

int main()
{
boost::asio::ip::tcp::resolver::query query("www.google.com", "80");
resolver.async_resolve(query, resolve_handler);
io_service.run();
}



[/code]

read_handler is first called connect_handler after write(), and then read_handler() repeatedly called inside read_handler() itself. So, "read_some" means read little by little from a complete transfered data? In opposed to read() free function, read() actually blocks and read the whole stream into a buffer?

Share this post


Link to post
Share on other sites
read will fill entire requested size. So if one read()s into a buffer of size 1024, it will block/not return until 1024 bytes are read or end of stream is reached.

read_some will read up to 1024 bytes, but may return earlier. To read more, call read_some again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this