Bonjour Browser / Listener Connections frequently Fail

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...

Answered by DTS Engineer in 872222022

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"

Further testing here...I think this might be related to using a physical iPad along with an Xcode Simulator. Both are connected to XCode debugger, so I can view the logs. However, if I test this with two physical iPads, it seems to work perfectly. Haven't managed to trigger a fail state yet.

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"

Bonjour Browser / Listener Connections frequently Fail
 
 
Q