The only time I'm setting a port number is when I assign the
NWConnection
as follows
OK, this only sets the remote peer’s port. The local peer will use a dynamically assigned port.
And the hardware I am connecting to sends the response to port 9999 so there is really no avoiding that.
OK, I’m confused. If the code you posted works with 7777, then the 7777 service doesn’t send its response to port 7777 because that code doesn’t set the local peer’s port.
My best guess here is that your two services behave differently:
Port 7777 sends its response to the remote peer’s port, which is how things should work.
Port 9999 always sends its response to port 9999, which is deeply suboptimal. Then again, you did mention that this is the legacy service, and perhaps that’s why.
Regardless, if you need to bind the connection’s local port to 9999 you can do that using the
requiredLocalEndpoint
parameter.
let parameters = NWParameters.udp
parameters.requiredLocalEndpoint = NWEndpoint.hostPort(host: .ipv6(.any), port: 9999)
let conn = NWConnection(host: "192.168.4.1", port: 999, using: parameters)
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"