NWConnection EACCES on Mojave

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

NWConnection
isn’t really set up to do broadcast, or indeed multicast, so there’s no special flag you can set to make this work. Actually, I’m kinda surprised this works on 10.15.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
This is indeed functionality that was only added in Catalina. There isn't a workaround for NWConnection in Mojave. If you need broadcast functionality in Mojave, you may want to add a version check and use a socket when on Mojave or earlier.
NWConnection EACCES on Mojave
 
 
Q