I am using NWListener
and NWBrowser
to establish a NWConnection
between two local devices. I noticed that if I run the NWBrowser
client on the iOS Simulator and create a (single) connection for a result endpoint found by the browser, the NWListener
's newConnectionHandler
on the destination device is called with three different connections.
Is this a bug or just something unique to the simulator? Or should I prepared to handle this in general? And if so, should I just accept the first connection and reject subsequent ones, or pick the last one, or choose the best option amongst them (and if so, how)?
Here is an example of the three connections received by the NWListener
for the one NWConnection
attempt from the simulator:
Client connected: [C1 fe80::8bb:e5ff:fe18:bab%anpi0.57383 tcp, traffic class: 700, local: fe80::8bb:e5ff:fe18:b54%anpi0.56395, definite, attribution: developer, server, path satisfied (Path is satisfied), viable, interface: anpi0, scoped]
Client connected: [C2 fe80::53:558d:8f79:9a0c%en2.57384 tcp, traffic class: 700, local: fe80::897:b297:76e0:a53%en2.56395, definite, attribution: developer, server, path satisfied (Path is satisfied), viable, interface: en2, scoped]
Client connected: [C3 fe80::3c:91d7:b7e:7c2b%en0.57385 tcp, traffic class: 700, local: fe80::1cf2:d1b6:220d:4d8f%en0.56395, definite, attribution: developer, server, path satisfied (Path is satisfied), viable, interface: en0, scoped, ipv4, ipv6, dns]
It’s very common for a listener to receive multiple redundant connections due to the the client’s Happy Eyeballs implementation. My advice is for you to accept them all. The client will then drop all except the one that it wants to use.
From the listener’s perspective this is no different from you ignoring connections that are dropped immediately, and your listener has to do that anyway.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"