Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Based on code from a Boost example (http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/timeouts/blocking_tcp_client.cpp) I made a function read_expect() as follows:

std::string TcpClient::read_expect(const boost::regex & expected,
                                   boost::posix_time::time_duration timeout)
{
    // Set a deadline for the asynchronous operation. Since this function uses
    // a composed operation (async_read_until), the deadline applies to the
    // entire operation, rather than individual reads from the socket.
    deadline_.expires_from_now(timeout);

    // Set up the variable that receives the result of the asynchronous
    // operation. The error code is set to would_block to signal that the
    // operation is incomplete. Asio guarantees that its asynchronous
    // operations will never fail with would_block, so any other value in
    // ec indicates completion.
    boost::system::error_code ec = boost::asio::error::would_block;

    // Start the asynchronous operation itself. The boost::lambda function
    // object is used as a callback and will update the ec variable when the
    // operation completes. The blocking_udp_client.cpp example shows how you
    // can use boost::bind rather than boost::lambda.
    boost::asio::async_read_until(socket_, input_buffer_, expected, var(ec) = _1);

    // Block until the asynchronous operation has completed.
    do
    {
        io_service_.run_one();
    }
    while (ec == boost::asio::error::would_block);

    if (ec)
    {
        throw boost::system::system_error(ec);
    }

    // take the whole response
    std::string result;
    std::string line;
    std::istream is(&input_buffer_);
    while (is)
    {
        std::getline(is, line);
        result += line;
    }

    return result;
}

This works fine when I use it with a regular expression

boost::regex(".*#ss#([^#]+)#.*")

, but I get strange behaviour when I change the regex to

boost::regex(".*#ss#(<Stats>[^#]+</Stats>)#.*")

, resulting in no timeout, causing the thread to hang on the async_read_until() call. Maybe this is some silly error I cannot see in the regex, and I could use the first version, but I would really like to know why this is happening.

Thanks for any insight, Marleen

share|improve this question
something went wrong while posting this question: the problematic regex should read boost::regex(".*#ss#(<Stats>[^#]+</Stats>)#.*") – user2025465 Jun 6 at 11:00
You have to escape the / character in the </Stats>. The correct expression: boost::regex(".*#ss#(<Stats>[^#]+<\\/Stats>)#.*") – pogorskiy Jun 6 at 11:22
Seems regex is very strange delimiter for async_read_until.. But, i think its simple format mistake, maybe about case-sensitiveness. – PSIAlt 8 hours ago

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.