问题
I have a simple boost::asio::ip::tcp::acceptor
which does almost nothing - it accepts connections in an infinite loop. I then have a number of connectors running at the same time trying to connect...
pSocket->async_connect(endpoint,
[=](boost::system::error_code error)
{
if(!error)
{
boost::asio::ip::tcp::no_delay noDelay(true);
pSocket->set_option(noDelay, error);
assert(!error);
std::cout << error.message() << '\n'; // "An invalid argument was supplied"
}
});
Everything is running in infinite loops and I'm running 2 clients and 1 server, all loopback connections. After a while (hundreds of successful connects and disconnects) when setting the no_delay
option on the connected socket I get the error An invalid argument was supplied
.
Under what conditions can setting an option on a socket cause this error? Has anyone seen this before, and know why it's happening and/or a way to fix it?
Update:
If I change the set set_option
to something like...
do
{
pSocket->set_option(noDelay, error);
} while(error);
...it will succeed - usually on the second try after it fails.
回答1:
Yesterday i debug set_option step by step, problem that set_option uses io_service_impl object but it contains bad socket pointer, that's why it crushes. But on socket creation nothing is set this socket, so i think you cant set option on socket in this way.
You must open socket before set_option, try this before async_connect
and set_option
:
sock->open(boost::asio::ip::tcp::v4());
Error is called Bad file descriptor
which direct on non-initialized socket on set_option call.
来源:https://stackoverflow.com/questions/12844608/boostasio-set-option-error