Creating another nw_endpoint_t/nw_connection_t from an existing one, to change port number

I'm able to discover a service with Bonjour, which gets me an nw_browse_result_t from which I can get an nw_endpoint_t and then an nw_connection_t. That's all fine. But this particular service runs on 3 ports. The port numbers of the other 2 ports are in the txt record (but they are well-known and stable anyway).

How can I create 2 more nw_connection_t to the same host/IP but on a different port?

I already have this working with NSNetService, but am trying to update to Network.framework.

I've found nw_endpoint_get_address() but the docs say it returns null "if the endpoint is not of type nw_endpoint_type_address" and indeed nw_browse_result_t gives me an nw_endpoint_type_bonjour_service.

Answered by DTS Engineer in 831843022

The way to handle this is to get the remote IP address from your connection and then create an endpoint with that IP address and the updated port. Do do this:

  1. Wait for the connection to go into the .ready state.

  2. Get the currentPath property.

  3. Get its remoteEndpoint property.

  4. Create a .hostPort(…) endpoint with that host and the desired port.

Sorry this is cast in terms of the Swift API, but that’s the one I’m most familiar with. Most folks using the C API can do the mapping. If you run into problems with that, post back here and I’ll be less lazy (-:

Share and Enjoy

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

The way to handle this is to get the remote IP address from your connection and then create an endpoint with that IP address and the updated port. Do do this:

  1. Wait for the connection to go into the .ready state.

  2. Get the currentPath property.

  3. Get its remoteEndpoint property.

  4. Create a .hostPort(…) endpoint with that host and the desired port.

Sorry this is cast in terms of the Swift API, but that’s the one I’m most familiar with. Most folks using the C API can do the mapping. If you run into problems with that, post back here and I’ll be less lazy (-:

Share and Enjoy

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

Creating another nw_endpoint_t/nw_connection_t from an existing one, to change port number
 
 
Q