For outgoing connections you should do the standard DNS dance:
resolve the DNS name
for each address you get back…
try to connect (using IPv4 or IPv6 depending on the address)
if that fails, move on to the next one
Once the connection is in place you:
know whether IPv4 or IPv6 was used
have the correct local address (always with TCP; with UDP, use a connected socket to get this)
can monitor the reachability of that local address use
SCNetworkReachabilityCreateWithAddressPair
For TCP you might consider using NSStream and then extracting the file descriptor from that object once the connection is in place. NSStream uses the system’s connect-by-name infrastructure, which contains smarts that are hard to replicate otherwise.
For UDP you don’t have that option, in which case you’ll want to read up on Happy Eyeballs. Be aware that the system’s connect-by-name support is a step beyond that.
For incoming connections, bring up listening sockets (well, for UDP that means a bound socket) on both
INADDR_ANY
(IPv4) and
in6addr_any
(IPv6). Any other approach requires you to monitor the address list and that’s quite painful.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"