Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the boost server class throwing runtime error

I am getting a compile error in the following code:

 boost::asio::io_context io_context;
 server server1(io_context, 1980);
 boost::thread t(boost::bind(&boost::asio::io_context::run, &io_context));

Where the definition of the server class is:

using boost::asio::ip::tcp;
class server
{
public:
    server(boost::asio::io_context& io_context, short port)
        : acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
    {
        do_accept();
    }

private:
    void do_accept()
    {
        acceptor_.async_accept(
            [this](boost::system::error_code ec, tcp::socket socket)
            {
                if (!ec)
                {
                    std::make_shared<session>(std::move(socket))->start();
                }

                do_accept();
            });
    }

    tcp::acceptor acceptor_;
};

The error is at the point:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

server(boost::asio::io_context& io_context, short port)
        : acceptor_(io_context, tcp::endpoint(tcp::v4(), port))

Exception thrown at 0x00007FFE9D804F69 in Everest.exe: Microsoft C++
exception: boost::wrapexceptboost::system::system_error at memory
location 0x0000003AA613BDB0.

>Solution :

It seems your code is missing a listen. On my Linux box this simply doesn’t do anything, but perhaps on Win32 it throws an error due to invalid state of the acceptor when doing async_accept?

Here’s a simple tester that does what is expected on linux:

#include <boost/asio.hpp>
#include <iostream>
using boost::asio::ip::tcp;

struct session : std::enable_shared_from_this<session> {
    tcp::socket _socket;

    session(tcp::socket&& s) : _socket(std::move(s)) {
        std::cerr << "new session " << _socket.remote_endpoint() << std::endl;
    }
    void start() {}
};

class server {
  public:
    server(boost::asio::io_context& io_context, short port)
        : acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
    {
        acceptor_.listen();
        do_accept();
    }

  private:
    void do_accept()
    {
        acceptor_.async_accept(
            [this](boost::system::error_code ec, tcp::socket socket) {
                std::cerr << "accept " << ec.message() << std::endl;
                if (!ec) {
                    std::make_shared<session>(std::move(socket))->start();
                    do_accept();
                }
            });
    }

    tcp::acceptor acceptor_;
};

int main()
{
    boost::asio::io_context io_context;
    server                  server1(io_context, 1980);

    io_context.run();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading