We have an application design where, every instance (process) is acting as a UDP server as well as UDP client, using the same UDP port: to listen & respond (as a server) to multiple destinations as well to send (as a client) to multiple destinations. This considering the implicit nature of UDP being connectionless. At any given point in time, I would be, as a server, talking to many clients and as a client, talking to many servers.
We were using BSD sockets for the purpose across all our target platforms including Apple Kernel (macOS, iOS, iPadOS, tvOS, watchOS etc.). Then we learnt about limitation on watchOS, where we started exploring 'Network Framework' as an alternative to BSD sockets on watchOS (or even others on Apple Kernel).
This is to understand, how can we achieve the same (if at all) using 'Network Framework'?
Process A
- [To act as UDP server] We will have NWListener on inaddrany, local port X, using UDP
- Does it implicitly work for both IPv4 and IPv6 incoming data?
- In case of BSD sockets, we would have created two sockets - one bound on INADDR_ANY and other on in6addr_any.
- Does in case of NWListener, also internally it creates two sockets - one for IPv4 and other for IPv6?
- For every incoming data from a client (which may not be on Apple Kernel and hence not using NWConnection), a NWConnection would be created on this UDP server (off course, if NWConnection does not already exist for the same local and remote IP/Port). Just for our clarity:
- An underlying socket is not created (like it would have in case of TCP)?
- The underlying data exchange between the UDP clients and this UDP server would happen on the same socket bound on port X?
- NWConnection for UDP is more a logical construct created to represent a “UDP flow”, that is, a sequence of datagrams, including both inbound and outbound, that share the same local IP / port and same remote IP / port tuple, where for 'local IP/Port', there would a socket bound on it internally.
- I can use the same NWConnection to respond (send data) back to the client.
- Since UDP is connectionless, how do we manage the lifecycle of these NWConnection(s) getting created?
- Though there is no socket resource to be freed per NWConnection basis BUT there must be some other system resources like memory being occupied.
- We understand that once cancelled, if we receive a datagram from the same client (actually, on the same UDP flow), the listener will create a new connection.
- [To act as UDP client] We will have to create a NWConnection to a UDP server
- We would like to have that NWConnection internally use the same local port X to send data to the remote UDP server, is that possible? The interface to init NWConnection seem to only take remote endpoint as an input and protocol as an input.
- And this we would like to do for all UDP servers we want to connect as client? Which would mean multiple NWConnection - one for each UDP Server we want to communicate to BUT same local port X is being used on the UDP Client.
- I will receive the response from the Server also on the same NWConnection (if still active and not cancelled).
- The client cancels the NWConnection when no more intends to talk to the same UDP Server.