From 77c67a275d332c25d31c36022d3ded057eb0bba4 Mon Sep 17 00:00:00 2001 From: Yves Piguet Date: Wed, 24 Nov 2021 15:15:12 +0100 Subject: [PATCH] use only ipv4 if problem with ipv6 Signed-off-by: Michael Bonani --- aseba/thymio-device-manager/app_server.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/aseba/thymio-device-manager/app_server.h b/aseba/thymio-device-manager/app_server.h index 77a105a97..0561c165a 100644 --- a/aseba/thymio-device-manager/app_server.h +++ b/aseba/thymio-device-manager/app_server.h @@ -11,12 +11,19 @@ template class application_server { public: application_server(boost::asio::io_context& io_context, uint16_t port = 0) - : m_io_context(io_context), m_acceptor(io_context, tcp::endpoint(tcp::v6(), port)) { - // Make sure we accept both ipv4 + ipv6 - boost::asio::ip::v6_only opt(false); - boost::system::error_code ec; - m_acceptor.set_option(opt, ec); - m_acceptor.listen(boost::asio::socket_base::max_listen_connections); + : m_io_context(io_context), m_acceptor(io_context) { + try { + m_acceptor = tcp::acceptor(io_context, tcp::endpoint(tcp::v6(), port)); + // Make sure we accept both ipv4 + ipv6 + boost::asio::ip::v6_only opt(false); + boost::system::error_code ec; + m_acceptor.set_option(opt, ec); + } catch (std::exception &e) { + // IPv6 failed: try IPv4 + mLogInfo("IPv6 failed, switch to IPv4-only"); + m_acceptor = tcp::acceptor(io_context, tcp::endpoint(tcp::v4(), port)); + } + m_acceptor.listen(boost::asio::socket_base::max_listen_connections); } tcp::acceptor::endpoint_type endpoint() const {