Receive UDP data from several hosts

I use for my use case the Network.Framework API.

My use case is the following:
  1. start a UDP connection on a specific port

  2. send UDP data to this host (A)

  3. get maybe data (its's optional) from this host (A)

  4. and receive data (on same port as above) from another host (B) (must-have)

It's possible to build this scenario with Network.Framework?
With my current code I get no data from the another host (B), it's only possible to receive data from host (A).

Here is my code:
Code Block
let endpoint = NWEndpoint.hostPort(host: NWEndpoint.Host.init("1.2.3.4"), port: NWEndpoint.Port(rawValue: UInt16(35000))!)
let params = NWParameters(dtls: nil, udp: .init())
params.requiredLocalEndpoint = NWEndpoint.hostPort(host: .ipv4(.any), port: 40000)
connection = NWConnection(to: endpoint, using: params)
connection.start(queue: queue)
// after the connection is ready (in state update handler)
// I start the receive code like this:
connection.receiveMessage { (data, _, isComplete, error)


This code works fine for the steps 1-3 from my scenario, but not for the step 4. IMHO the NF-API create a point-to-point connection the remote site.

But I want a point-to-multipoint connection on my local site (receive data from two host or more).

Is this possible?

I’d like to clarify your requirements here. It sounds like you have a host A and you want to listen for UDP ‘connections’ from a number of different hosts (B1, B2, and so on). B1 sends a message to port 40000 on A. Host A receives that message and needs to get the source IP and source port. Host A then sends a message back to those, which B1 can then receive.

Is that right?

Is B1’s source port fixed? Or do you want the system to assign it an ephemeral port?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"


Is that right?

Yes, it's almost right.

Here it is in detail:
  • host A sends data to host B1

  • host B1 sends data to host A

  • host B2 sends data to hast A

host A: iOS device
host B1 + B2: external devices (not iOS)


Is B1’s source port fixed?

Yes it is. The ports for B1 + B2 are fixed.
The port on A is not fixed, but I have to tell this used port to the other machines.

My totally special requirement is to solve this issue with "only" one socket instance.
One instance for streaming data to B1 and receive data from B2 or B1.
I think with BSD sockets that is possible. Receive data from "any" and send data to specific machine.

I know that I could solve this issue with NWListener who listen on port xyz for data.
And send data after the connection handler setup is finished. But that would be 2 sockets in my opinion.

Or you have other ideas or suggestions?


I think with BSD sockets that is possible.

Certainly.

I don’t think there’s a way to make this work with Network framework. Host A can’t set up an NWListener to accept new connections from host B2 because the fixed port is already in use.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"

I don’t think there’s a way to make this work with Network framework.

Okay, I already feared it :-)

Host A can’t set up an NWListener to accept new connections from host B2 because the fixed port is already in use.

Yepp ... that exactly right.
My current workaround is to setup a connection (NWConnection) on an ephemeral system port (and tell this port to the other side).
And setup an additional NWListener on a fixed port (e.g. 40.000). So it's possible to receive data from B1 and B2.
But I think it's not really nice, it would be gracefully if I could solve this with one socket connection.

But many thanks for your feedback and help.
Receive UDP data from several hosts
 
 
Q