Boost.asio C + + Network programming Translator (10)

Source: Internet
Author: User

Read/write method

These methods read and write to a stream (which can be a socket, or other class that behaves like a stream):

Async_read (stream, buffer [, Completion],handler): This method asynchronously reads from a stream. At the end, the processing method is called. The format of the processing method is: void handler (const BOOST::SYSTEM::ERROR_ Code & err, size_tbytes);. You can choose to specify a finish method. The completion method is called after each read operation call succeeds, and then tells the Boost.asio whether the Async_read operation is complete (assuming it is not finished, it will continue to read). Its format is: size_t completion (const boost::system::error_code& err, size_tbytes_transfered). When this finish method returns 0 o'clock, we feel that the read operation is complete; Suppose it returns a non-0 value that represents the number of bytes that the next async_read_some operation needs to read from the stream.

There will be a sample to illustrate this in detail.

Async_write (stream, buffer [, Completion],handler): This method writes data asynchronously to a stream. The meaning of the parameters is the same as the async_read.

Read (stream, buffer [, completion]): This method synchronously reads data from a stream. The meaning of the parameters is the same as the async_read.

Rite (stream, buffer [, completion]): This method writes data synchronously to a stream. The meaning of the parameters is the same as the async_read.

°async_read (Stream,stream_buffer [, completion], handler)

°async_write (Strean,stream_buffer [, completion], handler)

°write (Stream, stream_buffer[, completion])

°read (Stream, stream_buffer[, completion])

First, be aware that the first parameter becomes a stream, not just a socket. This includes the socket but not the socket. For example, you can use a Windows file handle instead of a socket.

All read and write operations will end when the following conditions occur:

The available buffers are full (when read) or all buffers have been written (when written).

The Finish method returns 0 (assuming you have provided a method)

When an error occurs

The following code reads the data asynchronously from the middle of a socket until it reads ' \ n ':

Io_service Service;

Ip::tcp::socket sock (service);

Char buff[512];

size_t up_to_en

int offset = 0;

Stem::error_code &, size_t bytes) {

ter (const BOOST::S

Y

for (size_t i = 0; i < bytes; ++i)

if (buff[i + offset] = = ' \ n ')

return 0;

return 1;

}

void On_read (const boost::system::error_code &, size_t) {}

...

C_read (sock, buffer (buff), up_to_enter,on_read);

Asy

N

Boost.asio also provides some simple finish-processing functor functions:

? Transfer_at_least (N)

? transfer_exactly (N)

? Transfer_all (

)

Examples include the following:

Char buff[512];

void On_read (Constboost::system::error_code &, size_t) {}

Read 32 bytes

Async_read (sock, buffer (buff), transfer_exactly (+), on_read);

The 4 methods above do not use normal buffers, and the STL stream and stream buffers are complex using the Stream_buffer method inherited by the Std::streambuf class of the Boost.asio, the following are examples:

Io_service Service;

void On_read (streambuf& buf, Constboost::system::error_code &

size_t) {

Std::istream in (&BUF);

Std::string Line;

Std::getline (in, line);

Std::cout << ' first line: ' << line << Std::endl;

}

int main (int argc, char* argv[]) {

HANDLE file =::createfile ("Readme.txt", Generic_read, 0, 0,

Open_always, File_attribute_normal | File_flag_overlapped,

0);

Windows::stream_handle h (service, file);

Streambuf buf;

UF, transfer_exactly (256),

Async_read (H,

B

Boost::bind (On_read,boost::ref (BUF), _1,_2));

Service.run ();

}

Here, I show you a call to Async_read on a Windows file handle. We read the previous 256 characters and then save them to the buffer when the operation is finished. On_read is called, then creates a std::istream to pass the buffer, reads the first line (Std::getline), and finally outputs it to the command line.

Read_until/async_read_until method

These methods are read until the condition is met:

Async_read_until (Stream, Stream_buffer,delim, handler): This method initiates an asynchronous read operation. The read operation ends when a delimiter is read. Delimiters can be characters, std::string, or Boost::regex. The format of the processing method is: void handler (const Boost::system::error_code & err, size_tbytes);.

Async_read_until (Strem, Stream_buffer,completion, Handler): This method is the same as the previous method, but there is no delimiter, but a complete processing method. The format of the finished processing method is:pair<iterator,bool> completion (iterator begin, iterator end), in which the type of the iterator is buffers_iterator< Streambuf::const_buffers_type>. What you need to remember is that the type of the iterator is a random access iterator. You scan the entire interval (begin,end) and then think that the read operation should end. You will return a result pair, the first member is an iterator that points to the last character visited by this method, and the second member returns true if the read operation needs to end, otherwise false.

Read_until (Stream, Stream_buffer, Delim): This method runs a synchronous read operation with the same meaning as async_read_until.

Read_until (Stream, Stream_buffer,completion): This method runs a synchronous read operation with the same meaning as async_read_until.

The following example reads until a specified punctuation mark is read

Typedefbuffers_iterator<streambuf::const_buffers_type> iterator;

std::p air<iterator, bool>match_punct (iterator begin, iterator end) {

while (begin = end)

if (std::ispunct (*begin))

Return Std::make_pair (begin,true);

Return Std::make_pair (End,false);

}

void On_read (Constboost::system::error_code &, size_t) {}

...

Streambuf buf;

Async_read_until (sock, buf, Match_punct,on_read);

Suppose we want to read the end of a space, we need to change the last line to:

Async_read_until (sock, Buff, ', on_read);

Boost.asio C + + Network programming Translator (10)

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.