Hi all,
I’m writing an app that begins a NetService on tvOS and connects to it from iOS. I’m creating the service with a port number of 0 to get an automatic port and publishing with the .listenForConnections option. On the client (iOS) side, once I resolve an address for the service I use its getInputStream(_:outputStream:) method to get input and output streams set up. I create a background thread to handle callbacks on and schedule the streams on its RunLoop.
What I’m noticing is that I get a lot of stream errors with this setup. I’m using JSONSerialization to write JSON objects to the stream and for the most part it works, but when I get these errors I’d like to reconnect. It seems like I can’t just call getInputStream(_:outputStream:) again on the resolved service, as doing so gives me all kinds of errors about a socket not being available. My next thing to try will be to rediscover the service, using its TXT data to make sure it’s the same one, re-resolve the address, and try again.
I’m pretty new to Bonjour and there aren’t a whole lot of resources out there for how to handle stream errors. Am I on the right track? What kind of gotchas should I be aware of here (for instance, is there a hard number of clients that could conceivably connect to a NetService on an Apple TV)?