Boost.asio C + + Network programming translator (26)

Source: Internet
Author: User

boost.asio-Other featuresThis chapter tells us about some of the features that Boost.asio are not so well known. Standard stream and Streambuf objects are sometimes harder to use, but as you can see, they also have their benefits. Finally, you'll see the late-Boost.asio portal, which makes your asynchronous code very readable. This is a very amazing feature.
standard stream and standard I/O bufferbefore reading this section you need to know about STL stream and STL Streambuf objects. the Boost.asio supports two types of buffer when handling I/O operations:
    • Boost::asio::buffer (): This buffer is associated with a boost.asio operation (the buffer we use is passed to a boost.asio operation)
    • Boost::asio::streambuf: This buffer inherits from Std::streambuf and can be used in network programming with STL stream
throughout the book, the most common examples of the previous examples are as follows:
    1. size_t Read_complete (Boost::system::error_code, size_t bytes) {...}   Char buff[1024];   Read (sock, buffer (buff), read_complete);   Write (sock, buffer ("echo\n"));
In general, this can be used to meet your needs, and if you want to be more complex, you can use STREAMBUF to achieve it. This is the simplest and worst thing you can do with Streambuf objects:

Streambuf buf;

   Read (sock, buf);
this will read until the Streambuf object is full, and then because the Streambuf object can get more space by re-opening the space itself, it basically reads that the connection is closed. you can use Read_until to always read a specific string:

Streambuf buf;

Read_until (sock, buf, "\ n");

This example reads a "\ n" until it is added to the end of buffer and exits the Read method. to write something to a Streambuf object, you need to do something like this:
Streambuf buf;   Std::ostream out (&BUF);   Out << "echo" << Std::endl;   Write (sock, buf);
this is very intuitive; you pass your Streambuf object in the constructor to build an STL stream, write it to the message you want to send, and then use write to send the contents of buffer.
Boost.asio and STL streamBoost.asio has done a great job of integrating STL streams and networks. In other words, if you are already using STL extensions, you already have a lot of classes that overload the operators << and >>. Reading from sockets or writing them is as simple as walking in a park. Suppose you have the following code snippet:
struct person {       std::string first_name, last_name;       int age;
   };   std::ostream& operator<< (Std::ostream & out, const person & p) {
       Return out << p.first_name << "<< p.last_name <<" << p.age;   
std::istream& operator>> (Std::istream & In, Person & P) {
    Return in >> p.first_name >> p.last_name >> p.age;

}

sending this person over the network is as simple as the following code snippet:
Streambuf buf;   Std::ostream out (&BUF);   person p;   ... Initialize p out   << p << Std::endl;   Write (sock, buf);
the other part can also be read very simply:
Read_until (sock, buf, "\ n");   Std::istream in (&BUF);   person p;   In >> p;
The best part of using the Streambuf object, of course, including the std::ostream it writes and the Std::istream to read, is that your final encoding will be natural:
    • When writing something to send over the network, it is very likely that you will have more than one fragment of data. So, you need to add data to a buffer. If the data is not a string, you need to convert it to a string first. These actions are already done by default when using the << operator
    • Similarly, in another section, when reading a message, you need to parse it, that is, when you read the data to a fragment, if the data is not a string, you need to convert it to a string. This is done by default when you use the >> operator to read something.
The last thing to give is a very famous, cool trick to use the following code snippet to output the contents of the Streambuf to the console
    1. Streambuf buf;   ...   Std::cout << &buf << Std::endl; Output all content to the console
Similarly, use the following code snippet to convert its contents to a string:
    1. std::string to_string (Streambuf &buf) {       std::ostringstream out;       Out << &buf;       return Out.str ();

}


Boost.asio C + + Network programming translator (26)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.