From 8a5b5455736cfc40995ea8c6022a07f8b46030b0 Mon Sep 17 00:00:00 2001 From: Johnathon Slightham Date: Wed, 4 Mar 2026 06:31:27 -0500 Subject: [PATCH] Fix UDP transmit on Windows --- src/UDPClient.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/UDPClient.cpp b/src/UDPClient.cpp index 3ad248d..5deb9fd 100644 --- a/src/UDPClient.cpp +++ b/src/UDPClient.cpp @@ -85,7 +85,7 @@ int UDPClient::init() { mreq.imr_interface.s_addr = INADDR_ANY; #ifdef _WIN32 - // Get hostname, resolve to primary IPv4 (won't work for all cases) + // Get hostname, resolve to primary IPv4 char hostname[256]; gethostname(hostname, sizeof(hostname)); hostent *host = gethostbyname(hostname); @@ -103,6 +103,22 @@ int UDPClient::init() { deinit(); return -1; } + + in_addr tx_iface{}; + tx_iface.s_addr = mreq.imr_interface.s_addr; + if (setsockopt(m_tx_socket, IPPROTO_IP, IP_MULTICAST_IF, (char *)&tx_iface, sizeof(tx_iface)) < 0) { + spdlog::error("[UDP] Failed to set multicast TX interface"); + print_errno(); + deinit(); + return -1; + } + + // Set multicast TTL > 1 so packets leave the local subnet if needed (default is 1). + constexpr int mcast_ttl = 32; + if (setsockopt(m_tx_socket, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&mcast_ttl, sizeof(mcast_ttl)) < 0) { + spdlog::warn("[UDP] Failed to set multicast TTL"); + print_errno(); + } #else setsockopt(m_rx_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); #endif