Skip to content

Commit c265fe1

Browse files
committed
Fix binding multiple sockets failed on MacOS
1 parent 5f94d83 commit c265fe1

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

CHANGELOG

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
- Fix MacOS build
66
- Fix example log macros doesn't work without format args
77
- Added socket binding (lines 165-176) - Bind to a local address before setting multicast options. This is required on some platforms like macOS for multicast senders.
8-
- Fixed setsockopt calls (lines 180, 189, 203) - Changed from passing raw pointer to using reinterpret_cast<const char*>(&value). While this is technically the same thing, it's more explicit about the expected type.
8+
- Fixed setsockopt calls (lines 180, 189, 203) - Changed from passing raw pointer to using reinterpret_cast<const char*>(&value). While this is technically the same thing, it's more explicit about the expected type
9+
- Fix binding multiple sockts failed on MacOS
910

1011
#1.0.0.0 - [09/26/2025]
1112
- Initial Release

include/slick/socket/multicast_receiver_unix.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,18 @@ bool MulticastReceiverBase<DerivedT>::setup_multicast_options()
203203
if (setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
204204
{
205205
int error = errno;
206-
LOG_WARN("Failed to set address reuse. error={} ({})", error, strerror(error));
206+
LOG_WARN("Failed to set SO_REUSEADDR. error={} ({})", error, strerror(error));
207207
}
208+
209+
#ifdef SO_REUSEPORT
210+
// On BSD-derived systems (macOS, FreeBSD), SO_REUSEPORT is required
211+
// to allow multiple sockets to bind to the same port
212+
if (setsockopt(socket_, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse)) < 0)
213+
{
214+
int error = errno;
215+
LOG_WARN("Failed to set SO_REUSEPORT. error={} ({})", error, strerror(error));
216+
}
217+
#endif
208218
}
209219

210220
// Bind to the multicast port

tests/multicast_receiver_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,16 @@ TEST_F(MulticastReceiverTest, MultipleReceiversSameGroup) {
146146
// Test that multiple receivers can listen to the same multicast group
147147
auto receiver1 = std::make_unique<TestMulticastReceiver>("TestReceiver1", config_);
148148
auto receiver2 = std::make_unique<TestMulticastReceiver>("TestReceiver2", config_);
149-
150-
// Both should be able to start (due to reuse_address = true)
149+
150+
// Both should be able to start (due to reuse_address = true and SO_REUSEPORT)
151151
EXPECT_TRUE(receiver1->start());
152152
EXPECT_TRUE(receiver2->start());
153-
153+
154154
std::this_thread::sleep_for(std::chrono::milliseconds(100));
155-
155+
156156
EXPECT_TRUE(receiver1->is_running());
157157
EXPECT_TRUE(receiver2->is_running());
158-
158+
159159
receiver1->stop();
160160
receiver2->stop();
161161
}

0 commit comments

Comments
 (0)