Exemple #1
0
 void start_listening() {
     using boost::asio::ip::tcp;
     system::error_code error;
     tcp::resolver resolver(service_);
     tcp::resolver::query query(address_, port_);
     tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error);
     if (error) {
         NETWORK_MESSAGE("Error resolving address: " << address_ << ':' << port_);
         return;
     }
     tcp::endpoint endpoint = *endpoint_iterator;
     acceptor_.open(endpoint.protocol(), error);
     if (error) {
         NETWORK_MESSAGE("Error opening socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
         return;
     }
     socket_options_base::acceptor_options(acceptor_);
     acceptor_.bind(endpoint, error);
     if (error) {
         NETWORK_MESSAGE("Error binding to socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
         return;
     }
     acceptor_.listen(tcp::socket::max_connections, error);
     if (error) {
         NETWORK_MESSAGE("Error listening on socket: " << address_ << ':' << port_ << " -- reason: '" << error << '\'');
         return;
     }
     new_connection.reset(new sync_connection<Tag,Handler>(service_, handler_));
     acceptor_.async_accept(new_connection->socket(),
         boost::bind(&sync_server_base<Tag,Handler>::handle_accept,
                     this, boost::asio::placeholders::error));
     listening_ = true;
 }
void InitTCPServer(boost::asio::ip::tcp::acceptor& server, int server_port) {
  boost::system::error_code server_ec;
  boost::asio::ip::tcp::endpoint server_ep(boost::asio::ip::tcp::v4(),
                                           server_port);
  server.open(boost::asio::ip::tcp::v4(), server_ec);
  ASSERT_EQ(0, server_ec.value()) << "Could not open server acceptor";
  server.bind(server_ep, server_ec);
  ASSERT_EQ(0, server_ec.value()) << "Could not bind server acceptor";
  server.listen(boost::asio::socket_base::max_connections, server_ec);
  ASSERT_EQ(0, server_ec.value()) << "Server acceptor could not listen";
}
	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;
	}
 http_sync_server(endpoint_type const& ep,
         std::string const& root)
     : sock_(ios_)
     , acceptor_(ios_)
     , root_(root)
 {
     acceptor_.open(ep.protocol());
     acceptor_.bind(ep);
     acceptor_.listen(
         boost::asio::socket_base::max_connections);
     acceptor_.async_accept(sock_,
         std::bind(&http_sync_server::on_accept, this,
             beast::asio::placeholders::error));
     thread_ = std::thread{[&]{ ios_.run(); }};
 }
    /** 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();
    }
Exemple #6
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(); }};
 }
Exemple #7
0
 void start_listening() {
   using boost::asio::ip::tcp;
   system::error_code error;
   tcp::resolver resolver(service_);
   tcp::resolver::query query(address_, port_);
   tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error);
   if (error) {
     BOOST_NETWORK_MESSAGE("Error resolving address: " << address_ << ':'
                                                       << port_);
     boost::throw_exception(std::runtime_error("Error resolving address."));
   }
   tcp::endpoint endpoint = *endpoint_iterator;
   acceptor_.open(endpoint.protocol(), error);
   if (error) {
     BOOST_NETWORK_MESSAGE("Error opening socket: " << address_ << ':' << port_
                                                    << " -- reason: '" << error
                                                    << '\'');
     boost::throw_exception(std::runtime_error("Error opening socket."));
   }
   socket_options_base::acceptor_options(acceptor_);
   acceptor_.bind(endpoint, error);
   if (error) {
     BOOST_NETWORK_MESSAGE("Error binding to socket: "
                           << address_ << ':' << port_ << " -- reason: '"
                           << error << '\'');
     boost::throw_exception(std::runtime_error("Error binding to socket."));
   }
   acceptor_.listen(tcp::socket::max_connections, error);
   if (error) {
     BOOST_NETWORK_MESSAGE("Error listening on socket: "
                           << address_ << ':' << port_ << " -- reason: '"
                           << error << '\'');
     boost::throw_exception(std::runtime_error("Error listening on socket."));
   }
   new_connection.reset(new sync_connection<Tag, Handler>(service_, handler_));
   auto self = this->shared_from_this();
   acceptor_.async_accept(
       new_connection->socket(),
       [=] (boost::system::error_code const &ec) { self->handle_accept(ec); });
   listening_ = true;
 }
	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();
    }