Search code examples
c++boostboost-asiowebsocket++

Handle boost endpoint listen/run error


I can't get an error of endpoint if I set wrong/busy port. I use websocket server that is publicly inherited from boost endpoint. I want handle error here:

try {
    m_server.reset(); // leads to some errors/warnings in console, but it works :)
    m_server.listen(777777777);
    m_server.start_accept();

    m_thread = std::thread(&Server::run, &m_server);

    return true;
}
catch (...) {
    m_running = false;
    return false;
}

I tried m_server.is_listening(), m_server.stopped() etc, but they show same result for both norm/bad port number. How can I handle error?

UPD1: I use websocketpp library


Solution

  • Try catching exceptions of type websocketpp::exception and calling the code() method on them. This should return a specific error code that you can inspect to see if it is a "wrong/busy port" or some other error.

    Alternatively, if you want to avoid exceptions, most methods (such as listen) will accept a second error code parameter and will fill that in with the error code that would have been set in the exception.