Skip to content

Commit

Permalink
Merge pull request #40 from Auterion/udp-multicast
Browse files Browse the repository at this point in the history
udp-server: add possibility to join multicast groups
  • Loading branch information
KonradRudin authored Feb 27, 2024
2 parents 903a3f9 + 86b6aea commit a9f8403
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/mav/UDPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ namespace mav {
}
}

void joinMulticastGroup(const std::string& multicast_group, const std::string& local_address="") const {
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());
if (setsockopt(_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
::close(_socket);
throw NetworkError("Could not join multicast group", errno);
}
}

void stop() const {
_should_terminate.store(true);
if (_socket >= 0) {
Expand Down

0 comments on commit a9f8403

Please sign in to comment.