title: Check port availability with boost
body: <p>I'm trying to check whether port on my pc is available or not. But this code crashes after the <strong>acceptor.listen();</strong>. Crash stack trace stops at:</p>
  
  <pre><code> detail/impl/win_iocp_socket_service_base.ipp
  </code></pre>
  
  <p>My code is:</p>
  
  <pre><code>    try {
          boost::asio::io_service io_service;
          boost::asio::ip::tcp::acceptor acceptor(io_service);
          boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string("127.0.0.1"), port);
          acceptor.open(endpoint.protocol());
          acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(false));
          acceptor.bind(endpoint);
          acceptor.listen();
      }
      catch (...) {
          // port busy
      }
  </code></pre>
  
  <p>How can I check this with boost or where is the problem in my code?</p>
  
  <p>UPD1: I tried following code from <a href="https://stackoverflow.com/questions/33358321/using-c-and-boost-or-not-to-check-if-a-specific-port-is-being-used/33389163#33389163">here</a>:</p>
  
  <pre><code>bool port_in_use(unsigned short port) {
  using namespace boost::asio;
  using ip::tcp;
  
      io_service svc;
      tcp::acceptor a(svc);
  
      boost::system::error_code ec;
      a.open(tcp::v4(), ec) || a.bind({ tcp::v4(), port }, ec);
  
      return ec == error::address_in_use;
  }
  </code></pre>
  
  <p>but after function executes, my program crashes still in same place.</p>