NWEndpoint invalid, interface nil

Hello,
I'm just start learning swift and Xcode 12.0 and writing a very simple code for iPhone 6 running iOS 14.4 that sends 0 or 1 over UDP on the local network. I'm stuck at creating the NWEndpoint - the destination for my datagram. It's always invalid when debugging on the device, saying that interface is nil. I suspect that my app does not have access to the wifi interface but I can't figure out how to enable it. WiFi is on and has IP address in the same range 192.168.1.102. I read the FAQ about Local Network Privacy and can't see what do I need to do here.

        let ip4 = IPv4Address("192.168.1.44")!
        let host = NWEndpoint.Host.ipv4(ip4)

Thanks!
Answered by arkadip in 662867022
Thank you for looking into my problem and sorry for not submitting all the code. I used something very similar to yours and I now tried your sample with no luck.
Code Block
var data = Data(count: 0)
data.append(contentsOf: [1])
let ip4 = IPv4Address("192.168.1.44")!
let host = NWEndpoint.Host.ipv4(ip4)
let port = NWEndpoint.Port("4444")!
let endpoint = NWEndpoint.hostPort(host: host, port: port)
let udpOption = NWProtocolUDP.Options()
let params = NWParameters(dtls: nil, udp: udpOption)
var conn = NWConnection(to: endpoint, using: params)
conn.send(content: data, completion: NWConnection.SendCompletion.contentProcessed({ NWError in
NSLog("%@", "\(NWError)")
}))

I don't think the listener is required to receive a UDP packet but I tried running it as well. In my opinion the problem is the endpoint - it is marked as invalid in my debug session because interface is nil.
I also used a monitor
Code Block
var monitor = NWPathMonitor(requiredInterfaceType: .wifi)
monitor.start(queue: .main)

and in debug session it shows 0 available interfaces. But in the device log I see this message for my process:
Code Block
nw_path_evaluator_start [BFB74778-B32B-4F15-8A78-BC38C1918A0C <NULL> generic, multipath service: 1, indefinite]
path: satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns
Local Network Privacy could be an issue here, but I'd like to get a look at how you are creating your NWConnection? Also, do you have a listener on the other end that the connection can be made to? I created a NWConnection wrapper class this morning that uses UDP. Let me know how your connection compares.

Code Block swift
class UDPConnection {
private var udpConnection: NWConnection?
init(host: String, port: String) {
let udpOption = NWProtocolUDP.Options()
let params = NWParameters(dtls: nil, udp: udpOption)
guard let unwrappedPort = NWEndpoint.Port(port) else { return }
let nwEndpoint = NWEndpoint.hostPort(host: NWEndpoint.Host(host),
port: unwrappedPort)
udpConnection = NWConnection(to: nwEndpoint, using: params)
}
/* Proceed with all of the other required methods here */
}
/* Calling into this class */
let udpConnection = UDPConnection(host: "192.168.1.44", port: "8484", context: "UDPConnection")


Over on the listening side.
Code Block text
# Over on 192.168.1.44
% nc -lku 8484



Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Accepted Answer
Thank you for looking into my problem and sorry for not submitting all the code. I used something very similar to yours and I now tried your sample with no luck.
Code Block
var data = Data(count: 0)
data.append(contentsOf: [1])
let ip4 = IPv4Address("192.168.1.44")!
let host = NWEndpoint.Host.ipv4(ip4)
let port = NWEndpoint.Port("4444")!
let endpoint = NWEndpoint.hostPort(host: host, port: port)
let udpOption = NWProtocolUDP.Options()
let params = NWParameters(dtls: nil, udp: udpOption)
var conn = NWConnection(to: endpoint, using: params)
conn.send(content: data, completion: NWConnection.SendCompletion.contentProcessed({ NWError in
NSLog("%@", "\(NWError)")
}))

I don't think the listener is required to receive a UDP packet but I tried running it as well. In my opinion the problem is the endpoint - it is marked as invalid in my debug session because interface is nil.
I also used a monitor
Code Block
var monitor = NWPathMonitor(requiredInterfaceType: .wifi)
monitor.start(queue: .main)

and in debug session it shows 0 available interfaces. But in the device log I see this message for my process:
Code Block
nw_path_evaluator_start [BFB74778-B32B-4F15-8A78-BC38C1918A0C <NULL> generic, multipath service: 1, indefinite]
path: satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns
Also, when I added
Code Block
params.requiredInterfaceType = NWInterface.InterfaceType.wifi
params.prohibitExpensivePaths = true
params.preferNoProxies = true
params.expiredDNSBehavior = NWParameters.ExpiredDNSBehavior.allow
params.prohibitedInterfaceTypes = [.cellular]
conn.start(queue: .main)

before
Code Block
conn.send()

I got more messages in the device log:
Code Block
[C1 1559D6A0-7A59-4E36-82DF-795BADE05925 192.168.1.44:4444 udp, indefinite, context: Default Network Context, proc: C3B92A94-F29B-31B5-B9DB-3C788C3CA49E, required interface type: wifi, multipath service: aggregate, prohibit expensive, use awdl, prefer no proxy, prohibited types: cellular] start
[C1 192.168.1.44:4444 initial path ((null))] event: path:start @78.058s
[C18 192.168.1.44:4444 waiting path (satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns)] event: path:satisfied @0.000s, uuid: 89A0D3AF-CA93-4580-A1E0-FF4C565F8D99
[C18 192.168.1.44:4444 in_progress fallback (satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns)] event: fallback:start_primary @0.000s
nw_connection_report_state_with_handler_on_nw_queue [C18] reporting state preparing
conn prep
[C18.1 192.168.1.44:4444 initial path ((null))] event: path:start @0.000s
[C18.1 192.168.1.44:4444 waiting path (satisfied (Path is satisfied), viable, interface: en0, scoped, ipv4, dns)] event: path:satisfied @0.001s, uuid: 904EB2DB-8F2E-4D4B-A1BC-99EEF3E3B1E5
[C18.1 192.168.1.44:4444 in_progress channel-flow (satisfied (Path is satisfied), viable, interface: en0, scoped, ipv4, dns)] event: flow:start_nexus @0.001s
nw_flow_connected [C18.1 192.168.1.44:4444 in_progress channel-flow (satisfied (Path is satisfied), viable, interface: en0, scoped, ipv4, dns)] Output protocol connected
....
[C18.1 192.168.1.44:4444 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, scoped, ipv4, dns)] event: flow:finish_connect @0.007s
nw_connection_report_state_with_handler_on_nw_queue [C18] reporting state ready
conn ready
[C18 192.168.1.44:4444 ready fallback (satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns)] event: flow:finish_connect @0.007s
[C18 192.168.1.44:4444 ready fallback (satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns)] event: fallback:finish_primary @0.007s
[C18.1 192.168.1.44:4444 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, scoped, ipv4, dns)] event: flow:changed_viability @0.007s
[C18 192.168.1.44:4444 ready fallback (satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns)] event: flow:changed_viability @0.007s
nil

Hm... and when I clicked Continue in debug, it actually did send my UDP byte! Now to find the actual reason for not working... or perhaps I was not using tcpdump on the destination.

Anyway, thanks again for looking into my problem. I guess it got solved!
NWEndpoint invalid, interface nil
 
 
Q