Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3977338
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:54:42+00:00 2026-05-20T04:54:42+00:00

I have a boost::asio based server which works fine, except that I’m trying to

  • 0

I have a boost::asio based server which works fine, except that I’m trying to add a check that nothing else is accepting connections on the same port. If I create two of the servers, one of them consistently accepts the connections, but the other does not report any error ( which of the two accepts all connections appears to be random ).

The relevant bits of the server class ( it’s a template which uses a base class which has Accepted() and typedefs for the connection type to create ) are:

        MessageServer ( boost::asio::io_service &io, unsigned short port_num )
            : BaseServerType ( io ), acceptor_ ( io, boost::asio::ip::tcp::endpoint ( boost::asio::ip::tcp::v4(), port_num ) ) {
            Listen();
        }
        void Listen () {
            boost::system::error_code ec;
            acceptor_.listen ( boost::asio::socket_base::max_connections, ec );

            if ( !ec ) {
                start_accept();
            } else {
                // not reached even if a separate process 
                // is already listening to that port
                connection_pointer new_connection;
                BaseServerType::Accepted ( new_connection );
            }
        }

    private:
        void start_accept() {
            connection_pointer new_connection ( CreateConnection ( acceptor_.io_service() ) );

            acceptor_.async_accept ( new_connection -> Socket(),
                                     boost::bind ( &MessageServer::handle_accept, this, new_connection,
                                                   boost::asio::placeholders::error ) );
        }

        void handle_accept ( connection_pointer new_connection, const boost::system::error_code &error ) {
            if ( !error ) {
                BaseServerType::Accepted ( new_connection );
                new_connection -> Start();
                start_accept();
            } else {
                // never reached
                new_connection.reset();
                BaseServerType::Accepted ( new_connection );
            }
        }

        boost::asio::ip::tcp::acceptor acceptor_;

acceptor.listen() doesn’t throw either.

How is a failure to listen to a server port reported in boost::asio?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:54:43+00:00Added an answer on May 20, 2026 at 4:54 am

    boost::asio::ip::tcp::acceptor constructor you use has a default argument reuse_address = true. this sets SO_REUSEADDR socket option. disabling this option you’ll receive error on acceptor.listen(). more info about SO_REUSEADDR here

    Be aware that this option is treated differently on windows and linux

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.