PrivateData( int portNumber ) :
		m_success(false),
		m_endpoint(tcp::v4(), portNumber),
		m_service(),
		m_acceptor( m_service ),
		m_thread()
	{
		m_acceptor.open(  m_endpoint.protocol() );
		m_acceptor.set_option( boost::asio::ip::tcp::acceptor::reuse_address(true));
		m_acceptor.bind( m_endpoint );
		m_acceptor.listen();
		m_success = true;
	}
    /** Open a listening port.

        @param ep The address and port to bind to.

        @param ec Set to the error, if any occurred.
    */
    void
    open(tcp::endpoint const& ep, error_code& ec)
    {
        acceptor_.open(ep.protocol(), ec);
        if(ec)
            return fail("open", ec);
        acceptor_.set_option(
            boost::asio::socket_base::reuse_address{true});
        acceptor_.bind(ep, ec);
        if(ec)
            return fail("bind", ec);
        acceptor_.listen(
            boost::asio::socket_base::max_connections, ec);
        if(ec)
            return fail("listen", ec);
        do_accept();
    }
Example #3
0
 sync_echo_server(bool server, endpoint_type ep)
     : sock_(ios_)
     , acceptor_(ios_)
 {
     error_code ec;
     acceptor_.open(ep.protocol(), ec);
     maybe_throw(ec, "open");
     acceptor_.set_option(
         boost::asio::socket_base::reuse_address{true});
     acceptor_.bind(ep, ec);
     maybe_throw(ec, "bind");
     acceptor_.listen(
         boost::asio::socket_base::max_connections, ec);
     maybe_throw(ec, "listen");
     acceptor_.async_accept(sock_,
                            std::bind(&sync_echo_server::on_accept, this,
                                      beast::asio::placeholders::error));
     thread_ = std::thread{[&]{ ios_.run(); }};
 }
	inline server::server(const std::string& address, 
			      const std::string& port,
			      std::size_t thread_pool_size, 
			      request_handler& request_handler_)
	    : thread_pool_size_(thread_pool_size),
	      acceptor_(io_service_),
	      new_connection_(new connection(io_service_, request_handler_)),
	      request_handler_(request_handler_)
	{
	    // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
	    boost::asio::ip::tcp::resolver resolver(io_service_);
	    boost::asio::ip::tcp::resolver::query query(address, port);
	    boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
	    acceptor_.open(endpoint.protocol());
	    acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
	    acceptor_.bind(endpoint);
	    acceptor_.listen();
	    acceptor_.async_accept(new_connection_->socket(),
				   boost::bind(&server::handle_accept, this,
					       boost::asio::placeholders::error));
	}
    void start(const std::string & ip_addr, const std::string & port)
    {
        ip::tcp::resolver resolver(io_service_pool_.get_now_io_service());
        ip::tcp::resolver::query query(ip_addr, port);
        ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);

        boost::system::error_code ec;
        acceptor_.open(endpoint.protocol(), ec);
        if (ec) {
            // Open endpoint error
            std::cout << "async_asio_echo_serv_ex::start() - Error: (code = " << ec.value() << ") "
                      << ec.message().c_str() << std::endl;
            return;
        }

        boost::asio::socket_base::reuse_address option(true);
        acceptor_.set_option(option);
        acceptor_.bind(endpoint);
        acceptor_.listen();

        do_accept();
    }
Example #6
0
 void acceptor_options(boost::asio::ip::tcp::acceptor &acceptor) {
   acceptor.set_option(acceptor_reuse_address);
   acceptor.set_option(acceptor_report_aborted);
 }