本文主要是介绍Boost Asio总结(12)class basic_socket_acceptor,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 对应socket api的accept()函数,用于服务端在指定的端口接收连接。
eg.
boost::asio::ip::tcp::acceptor acceptor(my_context);boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 12345);acceptor.open(endpoint.protocol());acceptor.bind(endpoint);
1.1
template <typename Protocol, typename Executor>
class basic_socket_acceptor: public socket_base
{void listen(int backlog = socket_base::max_listen_connections){boost::system::error_code ec;impl_.get_service().listen(impl_.get_implementation(), backlog, ec);boost::asio::detail::throw_error(ec, "listen");}BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec){impl_.get_service().close(impl_.get_implementation(), ec);BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);}BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec){impl_.get_service().cancel(impl_.get_implementation(), ec);BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);}/// Set an option on the acceptor. template <typename SettableSocketOption>void set_option(const SettableSocketOption& option){boost::system::error_code ec;impl_.get_service().set_option(impl_.get_implementation(), option, ec);boost::asio::detail::throw_error(ec, "set_option");}void wait(wait_type w){boost::system::error_code ec;impl_.get_service().wait(impl_.get_implementation(), w, ec);boost::asio::detail::throw_error(ec, "wait");}template <typename WaitHandler>BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,void (boost::system::error_code))async_wait(wait_type w, BOOST_ASIO_MOVE_ARG(WaitHandler) handler){return async_initiate<WaitHandler, void (boost::system::error_code)>(initiate_async_wait(), handler, this, w);}template <typename Protocol1, typename Executor1>void accept(basic_socket<Protocol1, Executor1>& peer,typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0){boost::system::error_code ec;impl_.get_service().accept(impl_.get_implementation(),peer, static_cast<endpoint_type*>(0), ec);boost::asio::detail::throw_error(ec, "accept");}
这篇关于Boost Asio总结(12)class basic_socket_acceptor的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!