boost :: asio :: async_read_until problema del delimitador de línea de lectura

I'm adapting the asio chat client example from aquí 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

Entonces parece que el "\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" y "\r\n" also but neither patterns are picked up.

¿Alguien sabe qué podría estar causando esto?

preguntado el 02 de febrero de 12 a las 10:02

Este planteamiento de « async_timeout_tcp example addresses the issue outline above... -

Also discussed aquí -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.