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 complete processing 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 (it will continue to read if it is not completed). Its format is: size_t completion (const boost::system::error_code& err, size_tbytes_transfered). When this completion method returns 0 o'clock, we think the read operation is complete; if it returns a value other than 0, it represents the number of bytes that the next async_read_some operation needs to read from the stream.
There will be an example to illustrate this in detail.
Async_write (stream, buffer [, Completion],handler): This method writes data asynchronously to a stream. The meaning of the parameter is the same as the async_read.
Read (stream, buffer [, completion]): This method synchronously reads data from a stream. The meaning of the parameter is the same as the async_read.
Rite (stream, buffer [, completion]): This method writes data synchronously to a stream. The meaning of the parameter 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 just the socket. For example, you can use a Windows file handle instead of a socket.
All read and write operations end when the following conditions occur:
The available buffers are full (when read) or all buffers have been written (when written).
The completion processing method returns 0 (if you have provided such a method)
When an error occurs
The following code asynchronously reads data 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 completion-processing functor functions:
? Transfer_at_least (N)
? transfer_exactly (N)
? Transfer_all (
)
Examples are as follows:
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 very complex using the Stream_buffer method inherited by the Std::streambuf class of Boost.asio:
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. The delimiter can be a character, 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 processing method is completed in the format:pair<iterator,bool> completion (iterator begin, iterator end), where 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 range (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 accessed by this method; the second member returns true if the read operation needs to end, otherwise false.
Read_until (Stream, Stream_buffer, Delim): This method performs a synchronous read operation with the same meaning as the async_read_until.
Read_until (Stream, Stream_buffer,completion): This method performs a synchronous read operation with the same meaning as the 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);
If 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)