Historically the inability of BSD Sockets to activate WWAN was a major gotcha, which is why the documentation is littered with references to it. On modern systems, however, the WWAN network is pretty much pinned up by various system services (most notably push notifications).
OTOH, modern systems have added other reasons to use CFSocketStream, including:
These require a connect-by-name API and there's no equivalent to that at the BSD Sockets level. Rather, BSD Sockets requires you to call
getaddrinfo
and then write a loop over each returned address, opening and connecting the socket, until you get one that works. Doing that reasonably well requires some
ugly code, code that can’t be as good as the OS’s implementation (
this post describes some of the smarts used by the OS in this case).
One option in situations like this is to cut the ‘head’ off your POSIX sockets code and connect it to our connect-by-name API. That is:
create a stream pair using
+[NSStream getStreamsToHostWithName:port:inputStream:outputStream:]
set
kCFStreamPropertyShouldCloseNativeSocket
to false (important because of step 7)schedule the streams on the run loop in a custom run loop mode
call
-open
run the run loop in your custom run loop mode waiting for the streams to open
extract the underlying socket file descriptor from the socket stream pair using
kCFStreamPropertySocketNativeHandle
close the streams
return the file descriptor to your BSD Sockets based I/O code
This allows you to keep the bulk of your BSD Sockets code intact, while replacing the ugly bits of that code with ugly, but better, Apple specific code.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"