Writing a UDP server with Network framework

I need to write a UDP listener that will require an endpoint which can receive and respond to multiple remote endpoints. My biggest problem is how this functionality maps to the connection-oriented APIs in Network framework.

As I understand it, there are 3 key elements to work out:
  1. Create a connection to each remote host I wish to exchange data with. How do I specify that I want to use the same UDP "socket" for them -- just pass the same local address (after enabling reuse)?

  2. Install a listener callback, to accept new packets / connections as they come in.

  3. How should these be scheduled -- can I just keep them all on the same queue?

I've found a few pieces of sample code for using UDP (netcat, WWDC camera demo), but invariably they assume that there will only be one peer. Any strategies for managing the number of connections, and dependencies in between, would be helpful.

I need to write a UDP listener that will require an endpoint which can
receive and respond to multiple remote endpoints.

Who initiates each of these flows? The most common UDP setup for Network framework is as follows:
  1. The ‘server’ uses NWListener to ‘listen’ on a specific port.

  2. Each ‘client’ initiates a flow by sending a datagram to that port.

  3. Your code hears about this via the listener’s new-connection handler. This is given an NWConnnection which represents the flows of traffic for the local IP/local port/remote IP/remote port tuple.

  4. Your code can run this connection like it would any other.

Steps 3 and 4 can repeated for multiple ‘clients’, each represented at the listener by its own NWConnection.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thanks Quinn. I was able to push through and bootstrap something that seems to work. Leaving it here in case it helps someone else.



Writing a UDP server with Network framework
 
 
Q