Sending to Multicast Group, Receiving on own IP

Hey there. I've struggled now for some time to archieve this in Swift/iOS. The situation is as followed:

  1. The app is sending via a multicastGroup (NWMulticastGroup) on a specific Port say 4555 a udp message: "Hello".
(App) xx.x.x.101:Port=> "Hello" => Multicastgroup:4555 
  1. Device within the local Network, in this Group, receives and replies with "World" directly to the IP of the sender, and on the Port from the sender.
"Hello" received xx.x.x.105 sending "World" => x.x.x.101:Port
  1. The app should receive and add this Device (via Ip)

My Concept:

Start:

guard let multicast = try? NWMulticastGroup(for:
                              [ .hostPort(host: "multicast.ip.address", port: 4555) ] )
 let params = NWParameters.udp;
 params.allowLocalEndpointReuse = true;
    ......
// And then after starting the queue
multicast.send(content: msgData)    

Thats not the big deal. The App sends via a generic port, (e.g. 53222) so the network device receives and replies on this port directly (but not inside the multicast group).


First Question: Is there a way to set/get the sending Port of NWConnectionGroup.send ? So i could set up a Listener on this Port. (don't know if this really would work, in fact of time between sending to multicast , shutdown multicastGroupConnection and setting up NWListener and start receiving).

I tried setting the outgoing port like below But when i set the outgoing IP address like this, the message is not sent to the group. Devices don't geht the "Hello" message.

 params.allowLocalEndpointReuse = true;
    params.requiredLocalEndpoint = NWEndpoint.hostPort(host: "0.0.0.0", port: 45555)
     // Or this 
     params.requiredLocalEndpoint = NWEndpoint.hostPort(host:.ipv4(.any), port: 45555);

The next thing is, while i want to receive not via the group, I set up a listener (on the sending port) NWListener, to handle the replies. Somehow there is no way to start the listener while NWConnectionGroup is running. Everytime I get the message - Address is in use - even if i try to set allowLocalEndpointReuse.

Or did I miss something?

I spent several Days figure a way out , but i'm totally stucked. Is this scenario realizable with NWNetworking? May someone here help me?

Sending to Multicast Group, Receiving on own IP
 
 
Q