Reputation: 303
I have a TCP socket that I want to bind to a local endpoint.
I have 2 scenarios:
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
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