The following NWConnection code sending a UDP broadcast message works perfectly on Catalina, but fails with EACESS on Mojave. I am guessing that the socket broadcast flag is set on Catalina but not Mojave, is there a workaround to get this function on 10.14, or is broadcast in the Network framework broken in Mojave ?
import Network
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
var broadcastConnection: NWConnection?
func broadcast() {
let hostUDP: NWEndpoint.Host = "255.255.255.255"
let portUDP: NWEndpoint.Port = 9000
broadcastConnection = NWConnection(host: hostUDP, port: portUDP, using: .udp)
broadcastConnection?.stateUpdateHandler = { (newState) in
switch newState {
case .ready:
let packet = "Hello".data(using: .utf8)!
print("SEND ")
broadcastConnection?.send(content: packet, completion: NWConnection.SendCompletion.contentProcessed(({ (NWError) in
print("Sent: \(NWError?.debugDescription ?? "Success")")
broadcastConnection?.cancel()
broadcastConnection = nil
PlaygroundPage.current.finishExecution()
})))
default:
break
}
}
broadcastConnection?.start(queue: .global())
}
broadcast()
Result on Catalina:
SEND
Sent: Success
Result on Mojave:
SEND
Sent: POSIXErrorCode: Permission denied
Thanks for any insight
Cheers
Guy