jo_dman
jo_dman

Reputation: 303

how to differ a busy endpoint over a not connected one using boost asio

I have a TCP socket that I want to bind to a local endpoint.

I have 2 scenarios:

  1. Connecting the socket to a local endpoint which is not in use and not waiting for a connection.
  2. Connecting the socket to a local endpoint which is used / not properly closed on a previous run.

I used to initialize the acceptor using its constructor passing it the io_service and the endpoint, but when it tries to connect to the used endpoint, the acceptor crashed.

acceptor_(io_service_, endpoint)

The next thing I did is to change the constructor to only get the io_service (which promises the socket will not be opened).

acceptor_(io_service_)

I saw that asio has a bind() function that binds the socket with a local endpoint. The function bind() has a parameter that indicates if an error has occurred, but for both the scenarios above it returns the same error.

boost::system::error_code ec;
acceptor_.bind(endpoint, ec);

if(ec)
{
    // SOME ERROR NOTIFYING
// EDIT: error code returned is 10009
}

Is there a way to connect to the endpoint and getting different errors for this types of errors?

Upvotes: 1

Views: 2648

Answers (1)

jo_dman
jo_dman

Reputation: 303

The socket for the acceptor I used was really not valid.

according to boost's code (located at: boost asio implementation), I saw that the the constructor that receives both io_service and endpoint opens the socket using the protocol from the endpoint itself, and only then binds it with the acceptor.

the solution is to initialize the acceptor only with the io_service, then open the acceptor and then binding the endpoint to it.

thanks for comments!

Upvotes: 3

Related Questions