How can I create a simple server / client application using boost.asio?
I've been going over the boost.asio examples and I'm wondering why there isn't a simple server / client example that prints a string to the server and then returns a response to the client. I tried to change the echo server but I can't figure out what I am doing at all.
Can anyone find me a client template and a server template?
I would like to eventually create a server / client application that receives binary data and simply returns an acknowledgment to the client that the data has been received.
EDIT:
void handle_read(const boost::system::error_code& error,
size_t bytes_transferred) // from the server
{
if (!error)
{
boost::asio::async_write(socket_,
boost::asio::buffer("ACK", bytes_transferred),
boost::bind(&session::handle_write, this,
boost::asio::placeholders::error));
}
else
{
delete this;
}
}
This only returns "A" to the client.
Also in data_ I get a lot of weird characters after the answer itself.
These are my problems.
EDIT 2:
Ok, so the main problem is with the client.
size_t reply_length = boost::asio::read(s, boost::asio::buffer(reply, request_length));
Since this is an echo server, the ACK will only be displayed when the request is longer than three characters.
How can I overcome this?
I tried changing request_length to 4, but that only makes the client wait and do nothing.
a source to share
Eventually I found out that the problem was with this piece of code on the server:
void handle_read(const boost::system::error_code& error,
size_t bytes_transferred) // from the server
{
if (!error)
{
boost::asio::async_write(socket_,
boost::asio::buffer("ACK", 4), // replaced bytes_transferred with the length of my message
boost::bind(&session::handle_write, this,
boost::asio::placeholders::error));
}
else
{
delete this;
}
}
And in the client:
size_t reply_length = boost::asio::read(s,
boost::asio::buffer(reply, 4)); // replaced request_length with the length of the custom message.
a source to share
Echo client / server is a simple example. What areas do you face? The client should be simple enough as it uses blocking APIs. The server is a little more complex as it uses asynchronous APIs with callbacks. When you boil it down to basic concepts (session, server, io_service), it's pretty easy to understand.
a source to share