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 easy to read. This is an amazing feature.


standard stream and standard I/O buffer before you read 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 in the previous example are the following:
    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"));
This is usually used to satisfy your needs, assuming you want to be more complex. You can use STREAMBUF to achieve. This is the simplest and worst thing you can do with Streambuf objects:

Streambuf buf;

   Read (sock, buf);
this would have been read until the Streambuf object was full. Then, because the Streambuf object can get a lot of other space by opening space again, 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 sample always reads "\ n" until it is added to the end of buffer, and then 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. Other words. Assuming that you are already using STL extensions, you already have a lot of classes that overload operators << and >>. Reading from sockets or writing them is as simple as walking in a park.

If 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 easily read:
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 used to read it, is that your last encoding will be very 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 the data to a buffer. Assuming that 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

    • Same. In another section, when you read a message, you need to parse it, that is. When reading data from a fragment, assume that the data is not a string and 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. 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 of the content to the console
The same, 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.