gunterhager / UDPBroadcastConnection

Framework to send UDP broadcast messages and listen to responses using a Dispatch source.
MIT License
84 stars 28 forks source link

iOS 14.4.2 / iPhone X – UDP connection failed to send data: No route to host. sendingMessageFailed(code: 65) #25

Closed ctsrc closed 3 months ago

ctsrc commented 3 years ago

Hi,

I am trying to use the example code, but am getting the following error when I try to invoke sendBroadcast:

"UDP connection failed to send data: No route to host"
sendingMessageFailed(code: 65)
"Closing UDP socket"

In my app I have:

import SwiftUI

// ...

struct ContentView: View {
    // ...
    var conn: UDPBroadcastConnection
    // ...
    init () {
        // ...
        conn = try! UDPBroadcastConnection(
            port: 9012,
            handler: { (ipAddress, port, response) -> Void in
              print("Received from \(ipAddress):\(port):\n\n\(response)")
              },
            errorHandler: { (error) in
              print(error)
            })
    }

    var body: some View {
        // ...
        Button(action: {
            // ...
            do {
                try conn.sendBroadcast("This is a test!")
            } catch {
                print(error)
            }
        }, label: {
            Text("Send")
                .padding([.top, .bottom], 6)
                .padding([.leading, .trailing], 18)
                .background(Color.green)
                .foregroundColor(.white)
        })
        // ...
    }
}

This happens regardless of which port I try to use, and it happens even though I have set a string value for NSLocalNetworkUsageDescription in Info.plist and I say "OK" when I am prompted that my app "would like to find and connect to devices on your local network." along with the message that I set in Info.plist.

I am running the app on an iPhone X with iOS 14.4.2. I am connected to a WiFi network.

ctsrc commented 3 years ago

I think I found the answer.

https://developer.apple.com/videos/play/wwdc2020/10110/

At about the 6:35 mark in the video above they say that an app needs to have the Multicast Networking Entitlement.

One can apply for this for an app by filling out and submitting the form https://developer.apple.com/contact/request/networking-multicast

Do you know if this applies even for local testing of an App that I am not yet distributing through App Store? And is this the reason for the error I am getting?