Always getting error message
func connectSocket(){ let client = TCPClient(address: "192.168.240.1", port: 40200) switch client.connect(timeout: 200) { case .success: switch client.send(string: "GET / HTTP/1.0\n\n" ) { case .success: guard let data = client.read(1024*10) else { return } if let response = String(bytes: data, encoding: .utf8) { print(response)
}
case .failure(let error):
print(error)
}
case .failure(let error):
print(error)
}
}
I am always getting an error"UnknownError" whenever I am trying to run the code
When connecting, the underlying C-code is able to catch the following issues:
- Unresolvable hostname
- Host closed connection
- Timeout
Any issue other than these is not currently detectable by Swift as it is just discarded within C.
Are you able to connect to the server on 192.168.240.1:40200 via a command-line tool like nc to verify the server is indeed reachable?