Opened 8 years ago

Last modified 8 years ago

#10744 new Bugs

wrong error code when doing async_connect and the connection is refused

Reported by: Martin Bonetti <martin.bonetti@…> Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.56.0 Severity: Regression
Keywords: errror_code, async_connect Cc:

Description

When executing the following example the error_code provided is not correctly recognized as connection_refused.

This is because the error_code supplied is provided by GetLastError and the error_code expected is provided by WSAGetLastError.

#include <iostream>
#include "boost/asio.hpp"

using namespace boost::asio;

int main(int argc, const char* argv[]) {
   io_service io_service;
   ip::tcp::socket::endpoint_type endpoint(ip::address_v4::from_string("127.0.0.1"), 9999);
   ip::tcp::socket tcp_socket(io_service);

   tcp_socket.async_connect(endpoint, [](const boost::system::error_code& ec) {
      if (ec.value() != boost::asio::error::connection_refused) {
        std::cout << "Expected error code " << boost::asio::error::connection_refused
                  << " but got " << ec.value() << std::endl;
        } else {
           std::cout << "got error code " << ec.value() << std::endl;
        }
   });

   io_service.run();
   return 0;
}

It seems that that this behavior change to earlier boost version has something to-do with this commit: github.com/boostorg/asio/commit/0484963a55bf109353922e1d5fdc2a218995d9e7

On linux system this example works fine

Attachments (1)

connection_refused.cpp (665 bytes ) - added by Martin Bonetti <martin.bonetti@…> 8 years ago.

Download all attachments as: .zip

Change History (2)

by Martin Bonetti <martin.bonetti@…>, 8 years ago

Attachment: connection_refused.cpp added

comment:1 by hvemha@…, 8 years ago

This related to the errors from ConnectEx and not the use of GetLastError or WSAGetLastError.

After some testing, I found that ConnectEx will give:
ERROR_NETWORK_UNREACHABLE instead of WSAENETUNREACH
ERROR_HOST_UNREACHABLE instead of WSAEHOSTUNREACH
ERROR_CONNECTION_REFUSED instead of WSAECONNREFUSED
ERROR_SEM_TIMEOUT instead of WSAETIMEDOUT

The best solution is probably to map these errors to the portable codes in win_iocp_socket_connect_op::do_complete

Note: See TracTickets for help on using tickets.