Jump to content
  • Advertisement
Sign in to follow this  
Rickert

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

This topic is 2536 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 The Boost C++ Libraries - Chapter 7



#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();
}





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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!