I am planning to implement Peer to Peer data exchange between 2 iOS devices. I have the following queries.
- My devices are connected through Ethernet. I would prefer to use this route if possible before switching over to Wifi. I can see there is an option to use prohibitedInterfaceTypes but that doesn't guarantee the route to use Ethernet when there are multiple available.
- Does the connection automatically switch between ethernet and Wifi or does that have to be handled via isViable and betterPathAvailable?
- I'm unsure when I should be using a custom framing protocol. I just need to exchange codable objects between the devices. Is the custom protocol required only if I use TCP as the underlying protocol?
- Can NSURLSessionStreamTask be used for this use case?
My devices are connected through Ethernet. I would prefer to use this route if possible before switching over to Wifi. I can see there is an option to use prohibitedInterfaceTypes but that doesn't guarantee the route to use Ethernet when there are multiple available.
Network framework will general use the best path by default, based on its Happy Eyeballs implementation. I wouldn’t expect Wi-Fi to be faster (well, lower latency) than Ethernet but… *shrug*.
There isn’t a way to specify “use Ethernet even if it’s worse than Wi-Fi, but fallback to Wi-Fi if Ethernet doesn’t work at all”. Partly that’s because “doesn’t work at all” isn’t a reasonable concept in networking.
I can see a couple of ways you could implement this yourself:
-
Start a connection with a required interface type (
requiredInterfaceType
) of Ethernet and, if that fails, or doesn’t connect promptly, trying again without that constraint. -
Open the connection with no constraints and, if the path is over Wi-Fi, try opening a second connection with Ethernet as the required interface type.
Honestly though, I think you’d be better off letting Network frame do its thing and then see if that causes a problem in practice.
Does the connection automatically switch between ethernet and Wifi … ?
No. For TCP that’s not possible because a TCP connection is identified by the local IP / local port / remote IP / remote port tuple. If you change interface, you change local IP and you have a different connection.
You can get this sort of automatic switching behaviour with MPTCP. I’m not sure if Network framework supports that on the server side.
I'm unsure when I should be using a custom framing protocol. I just need to exchange codable objects between the devices. Is the custom protocol required only if I use TCP as the underlying protocol?
What else are you going to use?
You could, for example, use QUIC for this, and then use one stream per message. Whether that makes sense in your scenario is hard to say without more background info.
One option that I like a lot is WebSocket. It works on top TCP (and TCP+TLS) and Network framework supports it on both the client and server side.
Can NSURLSessionStreamTask be used for this use case?
That’s not a good option. Stream tasks only support the client side, so you’d have to implement the server in something else. And if you’re going to do that with, say, Network framework, you might as well use Network framework on both sides.
Also, URLSessionStreamTask
is not something we generally recommend. See TN3151 Choosing the right networking API.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"