Opened 9 years ago

Last modified 9 years ago

#9794 new Bugs

Calling async_read_some before(tcp::socket, ...) before tcp::socket::async_connect handler is called causes async_connect handler to indicate successful connect

Reported by: tim.pavlic@… Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.55.0 Severity: Problem
Keywords: Cc:

Description

Given the example in this ticket, and assuming that there is no service running on localhost:29873, I would expect the example to print:

Failed to read
Failed to connect

Instead I get:

Failed to read
Connected!
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

using namespace std;
using namespace boost;
using boost::asio::ip::tcp;

int main(int argc, char* argv[])
{
    asio::io_service ios;
    tcp::socket socket{ios};
    
    tcp::endpoint endpoint(asio::ip::address::from_string("127.0.0.1"), 29873);

    socket.async_connect(endpoint, [](const system::error_code& ec) {
        if ( !ec ) {
            cout << "Connected!" << endl;        
        } else {
            cout << "Failed to connect" << endl;
        }
    });

    asio::streambuf readBuffer;
    asio::async_read_until(socket, readBuffer, '\n', [](const system::error_code& ec, size_t size) {
        if ( !ec ) {
            cout << "Read " << size << " " << endl;
        } else {
            cout << "Failed to read" << endl;        
        }
    });
    
    ios.run();
}

I also have the same problem in boost 1.48.

I know it's not really logical to do what the example shows, but I would not expect the result it gives.

Change History (1)

comment:1 by tim.pavlic@…, 9 years ago

Sorry, I forgot to mention that I'm using:

gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1)

Note: See TracTickets for help on using tickets.