Hi everyone,
I’m working on an app where I need to separate IPv4 and IPv6 traffic on a specific port, let's say "X", using the Network Framework. However, I’ve run into a problem: it appears that I'm only able to open a single NWListener for a given port number. I was under the impression that I should be able to create distinct IPv4 and IPv6 listeners for the same port "X".
Here’s the sample code I’ve written:
var params: NWParameters
var l1: NWListener
var l2: NWListener
params = NWParameters.udp
let protocolOptions = params.defaultProtocolStack.internetProtocol! as NWProtocolOptions
let ipOptions = protocolOptions as! NWProtocolIP.Options
ipOptions.version = .v6
l1 = try NWListener(using: params, on: NWEndpoint.Port(rawValue: 54192)!)
l1.stateUpdateHandler = InternalListenerStateHandler
l1.newConnectionHandler = InternalNewConnectionHandler
l1.start(queue: .global())
ipOptions.version = .v4
l2 = try NWListener(using: params, on: NWEndpoint.Port(rawValue: 54192)!)
l2.stateUpdateHandler = InternalListenerStateHandler
l2.newConnectionHandler = InternalNewConnectionHandler
l2.start(queue: .global())
I’m trying to figure out why this approach isn’t working. Is there a way to manage both IPv4 and IPv6 traffic on the same port using the Network Framework, or is there something I’m overlooking in my setup?
Additionally, when I switch to the BSD framework, I can successfully open two sockets on the same port by setting the "IPV6_ONLY" property on the IPv6 socket.
Any insights or advice would be greatly appreciated!
Thanks,
Harshal