What high-level API should I use to manage UDP sockets in iOS?

In the chapter " Using Sockets and Streams" in the " Network Programming Topics Concept Guide ", Apple says:

Note. . The POSIX network does not activate the cellular radio station in iOS. For this reason, the POSIX network interface is generally discouraged by iOS.

Also in the chapter " Network Tips and Errors" in the " Networking Overview ", Apple says:

On iOS, using sockets directly using the POSIX or CFSocket functions does not automatically activate cellular modem devices or on demand VPNs.

Well, that’s why you can't use POSIX sockets or CFSocket on iOS, which is just a thin POSIX socket shell supporting an asynchronous network via RunLoops. No problems. But which API should you use if you need a UDP socket ?

Apple goes on to say in the β€œ Networking Tips and Handicaps ” chapter in β€œ Networking Overview ”:

Avoid resolving DNS names before connecting to the host

Ideally, there should be an API for managing UDP sockets other than the POSIX API and CFSocket, which accepts DNS names instead of IP addresses for the destination address.

I may be blind, but I can not find such an API. Any ideas?

Using any third-party API (not Apple) is not interesting, since such an API should be based either on the Apple API, in which case I can use this Apple API directly. Writing your own cover API around POSIX sockets is a piece of cake, I wrote so many socket wrappers before, I already know all the unpleasant pitfalls. However, I will not use the POSIX API, which is the original problem here.

+6
ios udp networking
source share
3 answers

I asked Apple the exact same question, and their answer is more or less such that there is no high-level interface for UDP sockets. Regardless of what Apple says in its manuals, when using UDP, either use POSIX sockets directly, in combination with an async manager, such as poll() or select() , or create a POSIX socket (possibly use bind() and / or connect() as needed), and then wrap it in a CFSocket object using CFSocketCreateWithNative() to get integration with RunLoop. This is the best API that exists. All higher-level APIs are for use only with TCP.

+1
source share

Apple has some sample code that describes how to use UDP. I have not tried it myself, but this should give you some good pointers:

https://developer.apple.com/library/mac/#samplecode/UDPEcho/Introduction/Intro.html

0
source share

tried to do the same, and although the documents say that the lower level API does not activate cellular radio and VPN on demand, it turns out that this is not entirely true for TCP connections.

In the case of UDP, this is true and your UDP packets are not sent most of the time. To solve this problem, just open the listening socket for TCP using the lower level API and this will activate cellular radio or VPN on demand and close the socket as soon as you are done.

For TCP, you can use low-level APIs for server-side code on iOS devices, and this will activate cellular radio or VPN on demand, but for client code on iOS devices, it is preferable to use the higher-level APIs that were provided. In any case, the radio is active, and you do not need to worry that packets are not being sent.

By the way, this is what I'm doing right now.

0
source share

All Articles