Reputation:
I've been trying to achieve that for the better part of the day, I'd honestly apreciate any help. Both of my apps, the client and the server started throwing "vector subscript out of range" exceptions.
How does one do this thing properly ?
Still trying to figure this out, anyone?
As far as I undestand I'm supposed to create a
boost::asio::basic_stream_socket stream;
And then call :
stream.send(boost::asio::buffer(data));
?
I suppose it's perfectly possible to do it asynchronously ?
What are the differences between :
basic_stream_socket::send()
vs. basic_stream_socket::write_some()
vs. basic_stream_socket::async_write_some()
?
basic_stream_socket::receive()
vs. baic_stream_socket::read_some()
vs. basic_stream_socket::async_read_some()
?
I'm assuming that send()
& receive()
are methods that I can call when I wish to make sure that 100% of data is sent/received - at the expense of blocking the socket?
I'm assuming that write_some()
& read_some()
are methods that I can call when I'm not sure if 100% of data is sent/received - while still blocking the socket ?
I'm assuming that async_read_some()
& async_write_some()
are methods that don't block the socket and they read/write whatever they can ?
Upvotes: 7
Views: 7436
Reputation: 591
I'm fairly new to boost asio too but here are my answers:
The functions with function names starting with async_ takes, amongst others, a callback function as argument which will be called when the operation finishes. That is, the function async_write_some() does not block but returns immediately and when the operation is finished, the callback function is invoked.
Your assumptions are correct, as far as I've understood. However, with the functions async_write_some etc. you have a callback function which takes the number of bytes transferred as argument.
For example, here I have a function which reads from a socket:
boost::asio::async_read(
*socket_,
boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
boost::bind(
&sender::handle_read_content,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
The callback function in there is handle_read_content, and it looks like this:
void sender::handle_read_content(
const boost::system::error_code& err,
std::size_t bytes_transferred
)
In the beginning I read this tutorial which was very good: Boost Asio
Upvotes: 0
Reputation: 27593
Assuming you created a stream and are trying to send the vector<char>
data:
stream.send(boost::asio::buffer(data));
See boost:asio::buffer.
Upvotes: 5