[Boost-bugs] [Boost C++ Libraries] #8995: async_connect reports "success" even if the connection is refused

Subject: [Boost-bugs] [Boost C++ Libraries] #8995: async_connect reports "success" even if the connection is refused
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2013-08-14 16:46:12


#8995: async_connect reports "success" even if the connection is refused
------------------------------+----------------------------
 Reporter: info@… | Owner: chris_kohlhoff
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: asio
  Version: Boost 1.54.0 | Severity: Problem
 Keywords: |
------------------------------+----------------------------
 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.

 {{{#!cpp
 #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;
 }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/8995>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:13 UTC