Boost C++ Libraries: Ticket #9794: Calling async_read_some before(tcp::socket, ...) before tcp::socket::async_connect handler is called causes async_connect handler to indicate successful connect https://svn.boost.org/trac10/ticket/9794 <p> Given the example in this ticket, and assuming that there is no service running on localhost:29873, I would expect the example to print: </p> <pre class="wiki">Failed to read Failed to connect </pre><p> Instead I get: </p> <pre class="wiki">Failed to read Connected! </pre><div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;istream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;ostream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/asio.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/bind.hpp&gt;</span><span class="cp"></span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="p">;</span> <span class="k">using</span> <span class="n">boost</span><span class="o">::</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="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</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">ios</span><span class="p">;</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">ios</span><span class="p">};</span> <span class="n">tcp</span><span class="o">::</span><span class="n">endpoint</span> <span class="n">endpoint</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">address</span><span class="o">::</span><span class="n">from_string</span><span class="p">(</span><span class="s">&quot;127.0.0.1&quot;</span><span class="p">),</span> <span class="mi">29873</span><span class="p">);</span> <span class="n">socket</span><span class="p">.</span><span class="n">async_connect</span><span class="p">(</span><span class="n">endpoint</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="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">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Connected!&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Failed to connect&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="p">});</span> <span class="n">asio</span><span class="o">::</span><span class="n">streambuf</span> <span class="n">readBuffer</span><span class="p">;</span> <span class="n">asio</span><span class="o">::</span><span class="n">async_read_until</span><span class="p">(</span><span class="n">socket</span><span class="p">,</span> <span class="n">readBuffer</span><span class="p">,</span> <span class="sc">&#39;\n&#39;</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="kt">size_t</span> <span class="n">size</span><span class="p">)</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">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Read &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">size</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Failed to read&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="p">});</span> <span class="n">ios</span><span class="p">.</span><span class="n">run</span><span class="p">();</span> <span class="p">}</span> </pre></div></div><p> I also have the same problem in boost 1.48. </p> <p> I know it's not really logical to do what the example shows, but I would not expect the result it gives. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9794 Trac 1.4.3 tim.pavlic@… Thu, 20 Mar 2014 05:03:35 GMT <link>https://svn.boost.org/trac10/ticket/9794#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9794#comment:1</guid> <description> <p> Sorry, I forgot to mention that I'm using: </p> <p> gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1) </p> </description> <category>Ticket</category> </item> </channel> </rss>