Multicast with different ports

Hello,

I do have a Hardware device which receives the data at port 1235 and sending at port 1234.

Now the iOS app has to listen to port 1234 and if send the data to 1235.

Following the guidance from Apple, I've implemented a quick multicast group.

Code for starting the group:

let multicast = try! NWMulticastGroup(for: [.hostPort(host: "224.0.0.1", port: 1234)])
group = NWConnectionGroup(with: multicast, using: .udp)

group?.setReceiveHandler(maximumMessageSize: 128) { message, data, b in
        log("🧑‍💻 Received message:")
}
group?.stateUpdateHandler = { (newState) in
    print("Group entered state \(String(describing: newState))")
}
group?.start(queue: .global())

Everything works fine when receiving data in iOS from the external device, but the quest is, how I can use the newly created group to send data from iOS to the external device? Especially if its using a different port, other than the one its listening to? Appreciate any help with this.

Do you need to multicast the data back to the accessory? Or can you unicast it?

Share and Enjoy

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

We will have to multicast it, as the user can have multiple Accessory devices listening to the same port, and we want to manipulate at the same time.

In that case you’ll need a second connection group for port 1235.

Share and Enjoy

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

Thank you for your reply.

I've tried this approach, but as soon as I'm doing something like

let multicast = try! NWMulticastGroup(for: [.hostPort(host: "224.0.0.1", port: 1235)])

It will show me an error telling me that the address is already in use.

The same is true with address already in use, when the user is changing the network from Accessory WiFi to Home Router and after that back. Trying to re-subscribe results in Address already in use.

Multicast with different ports
 
 
Q