SwiftSocket icon indicating copy to clipboard operation
SwiftSocket copied to clipboard

Unable to read response

Open Ketan-Wadyalkar opened this issue 7 years ago • 4 comments

     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.

Ketan-Wadyalkar avatar May 16 '18 06:05 Ketan-Wadyalkar

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.

papwalker avatar May 23 '18 07:05 papwalker

"Server is working perfectly on android client app"

fukemy avatar Sep 21 '18 03:09 fukemy

debug return readLen always = 0

fukemy avatar Sep 21 '18 03:09 fukemy

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.

David-Keeffe avatar Aug 14 '20 00:08 David-Keeffe