Boost C++ Libraries: Ticket #10744: wrong error code when doing async_connect and the connection is refused https://svn.boost.org/trac10/ticket/10744 <p> When executing the following example the error_code provided is not correctly recognized as connection_refused. </p> <p> This is because the error_code supplied is provided by <strong><a class="missing wiki">GetLastError</a></strong> and the error_code expected is provided by <strong>WSAGetLastError</strong>. </p> <div class="wikipage" style="font-size: 80%"><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">&quot;boost/asio.hpp&quot;</span><span class="cp"></span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="o">::</span><span class="n">asio</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="k">const</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">io_service</span> <span class="n">io_service</span><span class="p">;</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="o">::</span><span class="n">endpoint_type</span> <span class="n">endpoint</span><span class="p">(</span><span class="n">ip</span><span class="o">::</span><span class="n">address_v4</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">9999</span><span class="p">);</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">tcp_socket</span><span class="p">(</span><span class="n">io_service</span><span class="p">);</span> <span class="n">tcp_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">boost</span><span class="o">::</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="n">ec</span><span class="p">.</span><span class="n">value</span><span class="p">()</span> <span class="o">!=</span> <span class="n">boost</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">connection_refused</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;Expected error code &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">boost</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">connection_refused</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; but got &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">ec</span><span class="p">.</span><span class="n">value</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="k">else</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;got error code &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">ec</span><span class="p">.</span><span class="n">value</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="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></div><p> It seems that that this behavior change to earlier boost version has something to-do with this commit: <a class="missing wiki">github.com/boostorg/asio/commit/0484963a55bf109353922e1d5fdc2a218995d9e7</a> </p> <p> On linux system this example works fine </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10744 Trac 1.4.3 Martin Bonetti <martin.bonetti@…> Tue, 04 Nov 2014 08:56:58 GMT attachment set https://svn.boost.org/trac10/ticket/10744 https://svn.boost.org/trac10/ticket/10744 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">connection_refused.cpp</span> </li> </ul> Ticket hvemha@… Sun, 09 Nov 2014 13:41:54 GMT <link>https://svn.boost.org/trac10/ticket/10744#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10744#comment:1</guid> <description> <p> This related to the errors from <a class="missing wiki">ConnectEx</a> and not the use of <a class="missing wiki">GetLastError</a> or WSAGetLastError. </p> <p> After some testing, I found that <a class="missing wiki">ConnectEx</a> will give:<br /> ERROR_NETWORK_UNREACHABLE instead of WSAENETUNREACH<br /> ERROR_HOST_UNREACHABLE instead of WSAEHOSTUNREACH<br /> ERROR_CONNECTION_REFUSED instead of WSAECONNREFUSED<br /> ERROR_SEM_TIMEOUT instead of WSAETIMEDOUT<br /> </p> <p> The best solution is probably to map these errors to the portable codes in win_iocp_socket_connect_op::do_complete </p> </description> <category>Ticket</category> </item> </channel> </rss>