boost::asio::socket keep alive set_option 异常

boost::asio::socket keep alive set_option exception

我这样创建了一个套接字:

client<THeader>::client(boost::asio::io_service* io_service, std::string const & host, int port)
    :_host(host), 
    _port(port), 
    _socket(boost::asio::ip::tcp::socket(*io_service)), 
    _io_service(io_service)
{
    boost::system::error_code ec;
    boost::asio::socket_base::keep_alive option(true);
    _socket.set_option(option, ec);
    bool is_set = option.value();
}

is_settrue。但是 ec 有一个非零值。我怎么理解这个?不过我之后可以使用套接字。看起来它工作正常。

ec.what() = set_option: The file handle supplied is not valid

您需要open the socket before you can use it. Before you call open不创建实际的底层套接字描述符。

或者使用 constructor that both create the object and opens the socket