Boost C++ Libraries: Ticket #8995: async_connect reports "success" even if the connection is refused https://svn.boost.org/trac10/ticket/8995 <p> While the synchronous (blocking) connect correctly reports "Connection refused", the asynchronous call always results in "Success". </p> <p> 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. </p> <p> Because the async_connect "is successful" a following write attempt fails with "broken pipe" (of course, there is no service running at this port). </p> <p> 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. </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/asio/connect.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/asio/ip/tcp.hpp&gt;</span><span class="cp"></span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">asio</span><span class="o">::</span><span class="n">io_service</span> <span class="n">io_service</span><span class="p">;</span> <span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="o">::</span><span class="n">resolver</span> <span class="n">resolver</span><span class="p">(</span><span class="n">io_service</span><span class="p">);</span> <span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="o">::</span><span class="n">socket</span> <span class="n">socket</span><span class="p">(</span><span class="n">io_service</span><span class="p">);</span> <span class="n">system</span><span class="o">::</span><span class="n">error_code</span> <span class="n">error</span><span class="p">;</span> <span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="o">::</span><span class="n">resolver</span><span class="o">::</span><span class="n">iterator</span> <span class="n">endpoint_iterator</span><span class="p">;</span> <span class="n">endpoint_iterator</span> <span class="o">=</span> <span class="n">resolver</span><span class="p">.</span><span class="n">resolve</span><span class="p">(</span><span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="o">::</span><span class="n">resolver</span><span class="o">::</span><span class="n">query</span><span class="p">(</span><span class="s">&quot;127.0.0.1&quot;</span><span class="p">,</span> <span class="s">&quot;1234&quot;</span><span class="p">));</span> <span class="n">connect</span><span class="p">(</span><span class="n">socket</span><span class="p">,</span> <span class="n">endpoint_iterator</span><span class="p">,</span> <span class="n">error</span><span class="p">);</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;synchronous connect status: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">error</span><span class="p">.</span><span class="n">message</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> <span class="n">error</span> <span class="o">=</span> <span class="n">asio</span><span class="o">::</span><span class="n">error</span><span class="o">::</span><span class="n">eof</span><span class="p">;</span> <span class="n">resolver</span><span class="p">.</span><span class="n">async_resolve</span><span class="p">(</span> <span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="o">::</span><span class="n">resolver</span><span class="o">::</span><span class="n">query</span><span class="p">(</span><span class="s">&quot;127.0.0.1&quot;</span><span class="p">,</span> <span class="s">&quot;1234&quot;</span><span class="p">),</span> <span class="p">[</span><span class="o">&amp;</span><span class="p">]</span> <span class="p">(</span><span class="k">const</span> <span class="n">system</span><span class="o">::</span><span class="n">error_code</span> <span class="o">&amp;</span><span class="n">ec</span><span class="p">,</span> <span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="o">::</span><span class="n">resolver</span><span class="o">::</span><span class="n">iterator</span> <span class="n">endpoint</span><span class="p">)</span> <span class="p">{</span> <span class="n">error</span> <span class="o">=</span> <span class="n">ec</span><span class="p">;</span> <span class="n">endpoint_iterator</span> <span class="o">=</span> <span class="n">endpoint</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">ec</span><span class="p">)</span> <span class="p">{</span> <span class="n">async_connect</span><span class="p">(</span> <span class="n">socket</span><span class="p">,</span> <span class="n">endpoint_iterator</span><span class="p">,</span> <span class="p">[</span><span class="o">&amp;</span><span class="p">]</span> <span class="p">(</span><span class="k">const</span> <span class="n">system</span><span class="o">::</span><span class="n">error_code</span> <span class="o">&amp;</span><span class="n">ec</span><span class="p">,</span> <span class="n">asio</span><span class="o">::</span><span class="n">ip</span><span class="o">::</span><span class="n">tcp</span><span class="o">::</span><span class="n">resolver</span><span class="o">::</span><span class="n">iterator</span> <span class="n">it</span><span class="p">)</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;asynchronous connect status: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">ec</span><span class="p">.</span><span class="n">message</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">);</span> <span class="n">io_service</span><span class="p">.</span><span class="n">run</span><span class="p">();</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8995 Trac 1.4.3 anonymous Tue, 20 Aug 2013 23:17:14 GMT <link>https://svn.boost.org/trac10/ticket/8995#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8995#comment:1</guid> <description> <p> Duplicate of issue: <a class="reopened ticket" href="https://svn.boost.org/trac10/ticket/8795" title="#8795: Bugs: async_connect incorrectly reports success on failure (reopened)">#8795</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Tue, 01 Oct 2013 10:47:10 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8995#comment:2 https://svn.boost.org/trac10/ticket/8995#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">duplicate</span> </li> </ul> Ticket