Opened 9 years ago

Closed 9 years ago

#8995 closed Bugs (duplicate)

async_connect reports "success" even if the connection is refused

Reported by: info@… Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.54.0 Severity: Problem
Keywords: Cc:

Description

While the synchronous (blocking) connect correctly reports "Connection refused", the asynchronous call always results in "Success".

The code below is a simple test case where I would expect that both ways result in "Connection refused" because there is no service running on the port 1234.

Because the async_connect "is successful" a following write attempt fails with "broken pipe" (of course, there is no service running at this port).

The problem gets worse if you replace "127.0.0.1" with "localhost" and you have a service running at 1234 using IPv4 but your system also supports IPv6. I would expect that the attempt on IPv6 fails but a second attempt on IPv4 succeeds. On my system "localhost" resolves first to IPv6 and the async_connect reports a successful connect which then fails with "broken pipe" on attempting to write to the socket. The connection using IPv4 is not established this way. Using the blocking call to "connect", everything works as expected.

#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>

using namespace boost;

int main()
{
        asio::io_service io_service;
        asio::ip::tcp::resolver resolver(io_service);
        asio::ip::tcp::socket socket(io_service);
        system::error_code error;
        asio::ip::tcp::resolver::iterator endpoint_iterator;

        endpoint_iterator = resolver.resolve(asio::ip::tcp::resolver::query("127.0.0.1", "1234"));
        connect(socket, endpoint_iterator, error);

        std::cout << "synchronous connect status: " << error.message() << std::endl;

        error = asio::error::eof;
        resolver.async_resolve(
                asio::ip::tcp::resolver::query("127.0.0.1", "1234"),
                [&] (const system::error_code &ec, asio::ip::tcp::resolver::iterator endpoint) {
                        error = ec;
                        endpoint_iterator = endpoint;
                        if (!ec) {
                                async_connect(
                                        socket,
                                        endpoint_iterator,
                                        [&] (const system::error_code &ec, asio::ip::tcp::resolver::iterator it) {
                                                std::cout << "asynchronous connect status: " << ec.message() << std::endl;
                                        }
                                );
                        }
                }
        );

        io_service.run();

        return 0;
}

Change History (2)

comment:1 by anonymous, 9 years ago

Duplicate of issue: #8795

comment:2 by chris_kohlhoff, 9 years ago

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.