I am using Apples Network framework to build a TCP connection to a server. It locks like this:
The serverAddress and serverPort is entered by the user. I want to test the connection. Now my problem is, that if the user is entering an invalid address / port combination (the service is not offered by the server). I stuck in the preparing state for quite a long time. After that I get to the waiting stage (with error message POSIXErrorCode: Operation timed out).
Is there any way to set the timeout for this first connection process ?
Thanks for your ideas
Code Block let testConnection = NWConnection(host: NWEndpoint.Host(server!.serverAddress), port: NWEndpoint.Port(server!.serverPort)!, using: .tcp) testConnection.stateUpdateHandler = ({ state in print("TCP state change to: \(state)") switch state { case .setup: break case .waiting(let error): print("Waiting Error \(error)") testConnection.cancel() break case .preparing: break case .ready: beginCommunication() break case .failed(let error): print("\(error)") break case .cancelled: break default: break } })
The serverAddress and serverPort is entered by the user. I want to test the connection. Now my problem is, that if the user is entering an invalid address / port combination (the service is not offered by the server). I stuck in the preparing state for quite a long time. After that I get to the waiting stage (with error message POSIXErrorCode: Operation timed out).
Is there any way to set the timeout for this first connection process ?
Thanks for your ideas
The NWConnection API is inherently asynchronous. If you want a custom timeout, just use a timer.
Having said that, I’m going to caution you against setting the timeout too low. There’s nothing more annoying that seeing a connection time out just as it was about to complete. The default timeouts are long (generally in the 30 to 75 second range) for a reason.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Having said that, I’m going to caution you against setting the timeout too low. There’s nothing more annoying that seeing a connection time out just as it was about to complete. The default timeouts are long (generally in the 30 to 75 second range) for a reason.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"