I have an app that is communicating with a non-HTTP server over TCP/IP. Most everything is working, but I was testing some error conditions and the first one I tried was turning the server off and then trying to send it a message.
I'm using code that uses NWConnection and involves an async method that includes a withCheckedContinuation. Inside this code are checks for errors in the closures, etc. You've seen the example code posted here in the forums. But none of the error code ever gets invoked. I also have a state handler to check the state of the TCP connection.
What I see when I send the request is:
- connection goes to .preparing state
- nothing happens for about 45 seconds
- I then get two errors:
inline-code
nw_endpoint_flow_failed_with_error [C4.1.1 192.168.86.44:3040 in_progress channel-flow (satisfied (Path is satisfied), viable, interface: en0[802.11], ipv4, dns, uses wifi)] already failing, returning
inline-code
nw_endpoint_flow_failed_with_error [C4.1.1 192.168.86.44:3040 cancelled channel-flow ((null))] already failing, returning
- then the connection state goes to .waiting
and nothing else happens. I would really like to capture the errors I see in the Xcode console, but I don't know how to catch them. Anyone have any ideas? Is there a better way to send the first message (or a ping or whatever) to a non-HTTP server and see if it is there?
Thanks, Robert
Network framework, being a relatively low-level API, tends not to apply its own policy to retries, timeouts, and so on. Instead you get the default policies from the underlying protocol, TCP in this case.
There’s a lot of general lore about how to manage a TCP connection correctly. Much of that isn’t specific to Network framework. For example, if you read a book about BSD Sockets and it says that you need to enable keepalives to ‘garbage collect’ TCP connections that have been zombified by an intervening NAT, you need to map the BSD Sockets constructs (the SO_KEEPALIVE
socket option and its friends) to Network frameworks constructs (the enableKeepalive
and its friends).
However, there are some cases where Network framework is unique, and this is one of them:
There’s no way to capture that specific text, but if the connection goes into in the .waiting(_:)
state then you can get the error from the associated NWError
value.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"