I've been working through some issues here attempting to migrate away from the MultiPeer Connectivity following the guidance in here, Moving from Multipeer Connectivity to Network Framework, and implementing a home-grown solution using NWBrowser and NWListener and NWConnections.
I feel like I am 95% of the way there, but am experiencing an issue where my connection attempts seem to fail about 50% of the time.
If I have two nodes say, one a physical iPad, and one a simulator on my Mac. I can start them both up, and they are both discovered with Bounjour no problem. Sometimes the "connect" no problem. Othertimes however when I attempt to "connect" from one device to the other, I get failures. I'd say it's about 50/50 success - fail at this point.
The "dialing" device state enters "preparing" but never progesses past that, and on the "receiving end" I see
receive error: POSIXErrorCode(rawValue: 61): Connection refused
I'm at my wits end here with this, everything else in my migration I think is done...just need some insights maybe on potential causes for that error...
First things first, for isolating issues like this I recommend that you disable peer-to-peer Wi-Fi, that is, comment out any code that sets includePeerToPeer. Peer-to-peer Wi-Fi is an added complexity that you don’t want to deal with when getting the basics to work.
I’m not aware of any problem that causes the specific symptom you’ve described, but I have some general debugging tips. Error 61 is ECONNREFUSED, which usually means that you’re connecting to the right host but:
- You have the wrong port.
- Or no one is listening on that port.
So, I recommend that you look at the port numbers in play. For example, if you’re using dynamic port assignment and start and stop the listener repeatedly — a common thing during debugging — then each listener gets its own port. If the client tries to connect based on stale information, you’ll see exactly this failure.
Usually Bonjour goes a good job of keeping all of its caches coherent, but this is a distributed system and thus there are certain fundamental challenges in play.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"