Unable to read response
let client = TCPClient(address: “xxx.xxx.x.x”, port: 5000)
switch client.connect(timeout: 10) {
case .success:
print(“Socket Connected")
switch client.send(data: jsonData ) {
case .success:
// This is always nil
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)
}
break
case .failure(let error):
print("Failure \(error.localizedDescription)")
break
}
I am able to connect to the socket successfully, but when i try to send data success block is getting called but response is always nil. Am i missing out on something? Socket server is in an android app and in response i should be receiving a string. Server is working perfectly on android client app.
It's a timing issue in the socket code. I get same result, but if I debug step through the code I get the response OK.
"Server is working perfectly on android client app"
debug return readLen always = 0
The issue is that without a timeout, client.read() is nonblocking. I had the same problem, but with a timeout, all worked well. If you look at the C code, you'll see that without a timeout, select is never called. The simplest fix would be to update the documentation.