Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which high-level API shall I use for managing UDP sockets on iOS? [closed]

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

Note: POSIX networking does not activate the cellular radio on iOS. For this reason, the POSIX networking API is generally discouraged in iOS.

Also in the chapter "Networking Tips and Pitfalls" of the "Networking Overview Guide", Apple says:

In iOS, using sockets directly using POSIX functions or CFSocket does not automatically activate the device’s cellular modem or on-demand VPN.

Okay, so on iOS one should neither use POSIX sockets nor CFSocket, which is just a thin wrapper around POSIX sockets which supports asynchronous networking via RunLoops. No problem. But what API should you use, if you need an UDP Socket?

Further Apple says in the the Chapter "Networking Tips and Pitfalls" of the "Networking Overview Guide":

Avoid Resolving DNS Names Before Connecting to a Host

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

Maybe I'm blind but I'm unable to find any such API. Any ideas?

Using any third party API (not from Apple) is not interesting, since such an API must base on either Apple API and in that case I can use this Apple API directly. Writing my own wrapper API around POSIX sockets is piece of cake, I've written so many sockets wrappers before, I already know all the nasty pitfalls. Yet I shall not use POSIX API, which is the initial problem here.

like image 442
Mecki Avatar asked Dec 18 '12 10:12

Mecki


People also ask

Does RECV work with UDP?

Unlike send(), the recv() function of Python's socket module can be used to receive data from both TCP and UDP sockets.

What does connect () do on a UDP socket?

Datagram (UDP) sockets. The CONNECT command enables an application to associate a socket with the socket name of a peer. The socket then is considered to be a connected UDP socket. You can call the CONNECT command multiple times with different peer names to change the socket association.

Does UDP have sockets?

User Datagram Protocol (UDP) socket processes, unlike TCP socket processes, are not clearly distinguished by server and client roles. The distinction is between connected and unconnected sockets.


2 Answers

I asked Apple exactly the same question and their reply is more or less that there isn't any high level interface for UDP sockets. Regardless of what Apple says in their guides, when using UDP, either use POSIX sockets directly, in combination with an async manager like poll() or select(), or create a POSIX socket (maybe use bind() and/or connect() on it as required) and then wrap it into a CFSocket object using CFSocketCreateWithNative() to get RunLoop integration. This is the best API that exists. All higher level APIs are designed to be used with TCP only.

like image 76
Mecki Avatar answered Sep 28 '22 18:09

Mecki


was attempting the same thing and although the docs say that lower level APIs will not activate the cellular radio and on-demand VPN, turns out its not entirely true for TCP connections.

In the case of UDP, this holds true and your UDP packets don't get sent most of the time. To solve this, just open up a listening socket for TCP using the lower level APIs and this will activate the cellular radio or on-demand VPN and close the socket once you are done.

For TCP, you can use the low-level APIs for server side code on iOS devices and this DOES activate the cellular radio or on-demand VPN but for client side code on iOS devices, it is preferable to use higher level APIs that have been provided. Either way, the radio is active and you don't have to worry about packets not being sent.

BTW, this is what I am currently doing.

like image 42
Shehzan Avatar answered Sep 28 '22 18:09

Shehzan