From 517e91a08d1dcd977ab9465053a21602c298e51c Mon Sep 17 00:00:00 2001 From: Daniel Mesham Date: Fri, 1 Mar 2024 10:06:24 +0000 Subject: [PATCH] udp-server: set REUSEADDR socket option when joining multicast group --- include/mav/UDPServer.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/mav/UDPServer.h b/include/mav/UDPServer.h index 9738bf4..b696c6a 100644 --- a/include/mav/UDPServer.h +++ b/include/mav/UDPServer.h @@ -77,6 +77,12 @@ namespace mav { } void joinMulticastGroup(const std::string& multicast_group, const std::string& local_address="") const { + int reuse = 1; + if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) { + ::close(_socket); + throw NetworkError("Could not join multicast group (could not enable SO_REUSEADDR)", errno); + } + struct ip_mreq mreq{}; mreq.imr_multiaddr.s_addr = inet_addr(multicast_group.c_str()); mreq.imr_interface.s_addr = local_address.empty() ? INADDR_ANY : inet_addr(local_address.c_str());