views:

290

answers:

2

I was going over the examples of boost.asio and I am wondering why there isn't an example of a simple server/client example that prints a string on the server and then returns a response to the client.
I tried to modify the echo server but I can't really figure out what I'm doing at all.
Can anyone find me a template of a client and a template of a server?
I would like to eventually create a server/client application that receives binary data and just returns an acknowledgment back to the client that the data is 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 returns to the client only 'A'.
Also in data_ I get a lot of weird symbols after the response itself.
Those 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 it's an echo server the 'ACK' will only appear whenever the request length is more then 3 characters.
How do I overcome this?
I tried changing request_length to 4 but that only makes the client wait and not do anything at all.

+1  A: 

The echo client/server is the simple example. What areas are you having trouble with? The client should be fairly straightforward since it uses the blocking APIs. The server is slightly more complex since it uses the asynchronous APIs with callbacks. When you boil it down to the core concepts (session, server, io_service) it's fairly easy to understand.

Sam Miller
I would like to print the string sent from the client on the server and then return an acknowledgment which will be printed on the client. Can you show me how it's done?
the_drow
A: 

Eventually I found out that the problem resides in this bit of code in 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.
the_drow