2

I'm adapting the asio chat client example from here to communicate with an existing client application which publishes line based data. Here is my code:

#include <cstdlib>
#include <deque>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>

boost::mutex global_stream_lock;
using boost::asio::ip::tcp;
typedef std::deque<std::string> simple_message_queue; 

class chat_client
{
public:
    chat_client(boost::asio::io_service& io_service,
                tcp::resolver::iterator endpoint_iterator)
    : io_service_(io_service),
    socket_(io_service)
    {
        if(DEBUGGING) std::cout << "[" << __FUNCTION__ << "]" << std::endl;
        boost::asio::async_connect(socket_, endpoint_iterator,
                                   boost::bind(&chat_client::handle_connect, this,
                                               boost::asio::placeholders::error));
    }

    void write(const std::string& i_msg)
    {
        io_service_.post(boost::bind(&chat_client::do_write, this, i_msg));
    }

    void close()
    {
        io_service_.post(boost::bind(&chat_client::do_close, this));
    }

private:

    void handle_connect(const boost::system::error_code& error)
    {
        if (!error)
        {
            boost::asio::async_read_until(socket_, simple_msg_buf_, "\n",
                                          boost::bind(&chat_client::handle_read_message, this,
                                                      boost::asio::placeholders::error,
                                                      boost::asio::placeholders::bytes_transferred));
        }
    }

    void handle_read_message(const boost::system::error_code& error, std::size_t bytes_transferred)
    {
        if (!error && bytes_transferred) 
        {
            // Remove newline from input.
            simple_msg_buf_.commit(bytes_transferred);

            std::istream is(&simple_msg_buf_);
            std::string s;
            is >> s;

            std::cout << s << std::endl;

            boost::asio::async_read_until(socket_, simple_msg_buf_, "\n",
                                        boost::bind(&chat_client::handle_read_message, this,
                                                    boost::asio::placeholders::error,
                                                    boost::asio::placeholders::bytes_transferred));
        }
        else
        {
            do_close();
        }
    }

    void do_close()
    {
        socket_.close();
    }

private:
    boost::asio::io_service& io_service_;
    tcp::socket socket_;
    boost::asio::streambuf simple_msg_buf_;
    simple_message_queue write_simple_msgs;
};

int main(int argc, char* argv[])
{
    try
    {
        boost::asio::io_service io_service;
        tcp::resolver resolver(io_service);
        tcp::resolver::query query("127.0.0.1", "20001");
        tcp::resolver::iterator iterator = resolver.resolve(query);

        chat_client c(io_service, iterator);

        boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));

        std::string input;
        while(std::cin)
        {
            std::getline(std::cin,input);

            // do something with input...
        }

        c.close();
        t.join();
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}

I have no problem communicating with the server but the format of the data I receiving is not what it should be. I want to parse the data line by line so I use a "\n" delimiter as I am doing so on Mac OS X (intel). For example, say I expect data of the format: This:(IS SOME) data; what I actually receive with the code above is of the form:

This:(IS
SOME)
data

So it appears that the "\n" character is treated in the same way as a whitespace (" "). In fact, if I replace the "\n" delimiter with a " ", the behavior is the same. I have also tried "\r" and "\r\n" also but neither patterns are picked up.

Does anyone know what might be causing this?

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
Pat Mustard
  • 1,852
  • 9
  • 31
  • 58
  • This [async_timeout_tcp](http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/timeouts/async_tcp_client.cpp) example addresses the issue outline above... – Pat Mustard Feb 08 '12 at 04:00
  • Also discussed [here](http://stackoverflow.com/questions/291871/how-to-set-a-timeout-on-blocking-sockets-in-boost-asio) – Pat Mustard Feb 08 '12 at 04:02

0 Answers0