From e639a3fd5e15445f32319f26936f0daf5fea0ca3 Mon Sep 17 00:00:00 2001 From: Bouke Haarsma Date: Thu, 30 Mar 2017 21:57:07 +0200 Subject: [PATCH] Set SO_REUSEPORT for port sharing on OSX --- Sources/Socket.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/Socket.swift b/Sources/Socket.swift index 6a31a78..2ea64b4 100644 --- a/Sources/Socket.swift +++ b/Sources/Socket.swift @@ -2020,7 +2020,15 @@ public class Socket: SocketReader, SocketWriter { throw Error(code: Socket.SOCKET_ERR_SETSOCKOPT_FAILED, reason: self.lastError()) } - + + // SO_REUSEPORT allows completely duplicate bindings by multiple processes if they + // all set SO_REUSEPORT before binding the port. This option permits multiple + // instances of a program to each receive UDP/IP multicast or broadcast datagrams + // destined for the bound port. + if setsockopt(self.socketfd, SOL_SOCKET, SO_REUSEPORT, &on, socklen_t(MemoryLayout.size)) < 0 { + throw Error(code: Socket.SOCKET_ERR_SETSOCKOPT_FAILED, reason: self.lastError()) + } + // Get the signature for the socket... guard let sig = self.signature else {