boost::asio set_option error

江枫思渺然 提交于 2019-12-22 10:41:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!